美文网首页iOSiOS 开发IOS进阶
iOS开发—判断NSString是否包含某个字符串

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

作者: 艾姆希 | 来源:发表于2016-03-23 09:59 被阅读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");

}

相关文章

网友评论

  • TinaAndNike:谢谢你,郁闷一下午了,看到你写的代码瞬间笑出来声。哈哈哈哈,谢谢~
  • 115a6027f65e:文明点多好呢

本文标题:iOS开发—判断NSString是否包含某个字符串

本文链接:https://www.haomeiwen.com/subject/fgmllttx.html