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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM