iphone - 如何检查字符串是否包含空格

标签 iphone ios nsstring whitespace

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

最佳答案

使用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/

相关文章:

iphone - 推送下一个 View 时的事件指示器 - didSelectRowAtIndexPath

ios - 如果我在 Realm 上创建数据库,我的用户是否可以在没有互联网访问权限的情况下访问它?

iphone - UISplitViewController 以编程方式

ios - 呈现和关闭最后一个 UIViewController

objective-c - 我的程序中的输入语句被忽略

ios - 检测空白

iphone - isa 是弃用问题

iphone - 在 CGAffineTransform 之后获取 UIView 的中心

ios - iPhone蓝牙和 "Made for iPod"

ios - 如何将我的设备 token (NSData) 转换为 NSString?