在Objective-C中,检测一个或多个空格的方法如下:
NSString *string = @"Hello World ";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\s+" options:0 error:&error];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
BOOL hasSpaces = (matches.count > 0);
rangeOfString
方法NSString *string = @"Hello World ";
NSRange range = [string rangeOfString:@" "];
BOOL hasSpaces = (range.location != NSNotFound);
componentsSeparatedByString
方法NSString *string = @"Hello World ";
NSArray *components = [string componentsSeparatedByString:@" "];
BOOL hasSpaces = (components.count > 1);
以上三种方法都可以用于检测一个或多个空格。如果需要检测其他空白字符,可以使用正则表达式的\\s
表示空白字符。
企业创新在线学堂
腾讯技术开放日
云+社区开发者大会 长沙站
Elastic 中国开发者大会
DB TALK 技术分享会
云+社区技术沙龙[第14期]
Elastic 中国开发者大会
腾讯云GAME-TECH沙龙
DB TALK 技术分享会
领取专属 10元无门槛券
手把手带您无忧上云