Mat*_*uch 49
NSString *foo = @"HALLO WELT";
NSRange whiteSpaceRange = [foo rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
if (whiteSpaceRange.location != NSNotFound) {
NSLog(@"Found whitespace");
}
Run Code Online (Sandbox Code Playgroud)
注意:这也会在字符串的开头或结尾找到空格.如果你不想首先修剪这个字符串......
NSString *trimmedString = [foo stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSRange whiteSpaceRange = [trimmedString rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
Run Code Online (Sandbox Code Playgroud)
您还可以按照以下步骤操作:
NSArray *componentsSeparatedByWhiteSpace = [testString componentsSeparatedByString:@" "];
Run Code Online (Sandbox Code Playgroud)
如果您的字符串中有任何空格,那么它将分隔这些空格并在数组中存储不同的组件.现在你需要计算数组.如果count大于1,则表示存在两个组件,即存在空白区域.
if([componentsSeparatedByWhiteSpace count] > 1){
NSLog(@"Found whitespace");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14758 次 |
| 最近记录: |