如何检查字符串中字符之间是否包含空格?

最佳答案

使用 rangeOfCharactersFromSet:

NSString *foo = @"HALLO WELT";
NSRange whiteSpaceRange = [foo rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
if (whiteSpaceRange.location != NSNotFound) {
    NSLog(@"Found whitespace");
}

注意:这也会在字符串的开头或结尾找到空格。如果您不希望先修剪字符串...
NSString *trimmedString = [foo stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSRange whiteSpaceRange = [trimmedString rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];

关于iphone - 如何检查字符串是否包含空格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8383034/

10-11 14:49
查看更多