开发者

is a character already in the range?

I need to get, is a character already in the range. The character string is of 开发者_开发百科type NSMuttableString. For example, I have a string of "52.648" and I need to know is a "." symbol is already in the string. How can I do it?


You can use the rangeOfString message on the NSString:

NSRange rng = [string rangeOfString:@"."];
if (rng.location != NSNotFound)
    // "." is in the string at position rng.location


The following expression is true if and only if myChar is within myString:

[myString rangeOfString: [NSString stringWithFormat: @"%c", myChar]
                options: NULL].location != NSNotFound


Just as addition to the other answers which are correct. I've had issues using NSNotFound. Not sure if its a bug or not but using range.length > 0 works as intended. I posted about it here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜