iOS開發—判斷NSString是否包含某個字串
阿新 • • 發佈:2019-01-28
在iOS8以後,還可以用下面的方法來判斷是否包含某字串:
NSString *women = @"Hey you are bitch ?";
if ([women containsString:@"bitch"]) {
NSLog(@"women 包含 bitch");
} else {
NSLog(@"women 不存在 bitch");
}
NSString *string = @"hello,fucking,you,bitch";
//字條串是否包含有某字串
if ([string rangeOfString:@"fucking"].location == NSNotFound) {
NSLog(@"string 不存在 fucking");
} else {
NSLog(@"string 包含 fucking");
}
//字條串開始包含有某字串
if ([string hasPrefix:@"hello"]) {
NSLog(@"string 包含 hello");
} else {
NSLog(@"string 不存在 hello");
}
//字串末尾有某字串;
if ([string hasSuffix:@"bitch"]) {
NSLog(@"string 包含 bitch");
} else {
NSLog(@"string 不存在 bitch");
}
文/艾姆希(簡書作者)
原文連結:http://www.jianshu.com/p/24d6dcf9ed0a
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。