美文网首页
iOS开发—判断NSString是否包含某个字符串的各种姿势

iOS开发—判断NSString是否包含某个字符串的各种姿势

作者: LWide | 来源:发表于2017-06-20 09:53 被阅读0次

//开头包含

if([webpageURL.absoluteString hasPrefix:@"a.com.cn/openApp"]) {

}

//结尾包含

if([webpageURL.absoluteString hasPrefix:@"/openApp"]) {

}

//字条串是否包含有某字符串

if([webpageURL.absoluteString rangeOfString:@"a.com.cn/openApp"].location==NSNotFound) {

}

//字条串是否包含有某字符串

if([webpageURL.absoluteString containsString:@"a.com.cn/openApp"]) {

}

主要用到三种方法来判断:

rangeOfString 是否包含

hasPrefix 是否在前缀包含

hasSuffix 是否在末尾包含

//判断字符是否包含某字符串;

NSString*string = @"hello,shenzhen,martin";

//字条串是否包含有某字符串

if([string rangeOfString:@"martin"].location==NSNotFound) {

NSLog(@"string 不存在 martin");

}else{

NSLog(@"string 包含 martin");

}

//字条串开始包含有某字符串

if([string hasPrefix:@"hello"]) {

NSLog(@"string 包含 hello");

}else{

NSLog(@"string 不存在 hello");

}

//字符串末尾有某字符串;

if([string hasSuffix:@"martin"]) {

NSLog(@"string 包含 martin");

}else{

NSLog(@"string 不存在 martin");

}

在iOS8以后,还可以用下面的方法来判断是否包含某字符串:

//在iOS8中你可以这样判断

NSString*str = @"hello world";

if([str containsString:@"world"]) {

NSLog(@"str 包含 world");

}else{

NSLog(@"str 不存在 world");

}

相关文章

网友评论

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

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