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

2025-11-21 19:18:37

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript复制//判断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”);

}

代码语言:javascript复制援引:https://www.cnblogs.com/qingjoin/archive/2012/12/14/2817690.html

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/154818.html原文链接:https://javaforall.cn

最新发表
友情链接