What's the fastest way to find the number of spaces in an NSString?
I have an NSString and I want to find the number of spaces in it. What's the fastest way?
也许不是最快的执行,但最快输入:
[[myString componentsSeparatedByString:@" "] count]-1
I tried this and it seems to work. It might be possible to optimize, but you only need to worry about that if it's absolutely necessary.
NSUInteger spacesInString(NSString *theString) {
NSUInteger result = 0;
if ([theString length]) {
const char *utfString = [theString UTF8String];
NSUInteger i = 0;
while (utfString[i]) {
if (' ' == utfString[i]) {
result++;
}
i++;
}
}
return result;
}
我没有测试过这个,但它似乎应该是我的头顶。
int spacesCount = 0;
NSRange textRange;
textRange = [string rangeOfString:@" "];
if(textRange.location != NSNotFound)
{
spacesCount = spacesCount++;
}
NSLog(@"Spaces Count: %i", spacesCount);
链接地址: http://www.djcxy.com/p/14926.html
上一篇: 在文本字段中显示Unicode字符