iOS 判断字符串中含有某个字符串 rangeOfString

//判断roadTitleLab.text 是否含有qingjoin
            if([roadTitleLab.text rangeOfString:@"qingjoin"].location !=NSNotFound)//_roaldSearchText
            {
                NSLog(@"yes");
            }
            else 
            {
                NSLog(@"no");
            }


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

}



 

援引:https://www.cnblogs.com/qingjoin/archive/2012/12/14/2817690.html

原文地址:https://www.cnblogs.com/gaozhang12345/p/8093640.html