iOSiOS 开发IOS进阶

iOS开发—判断NSString是否包含某个字符串

2016-03-23  本文已影响21110人  艾姆希

在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");

}

上一篇下一篇

猜你喜欢

热点阅读