判断NSString是否包含某个字符串

来源:互联网 发布:老程序员的出路 编辑:程序博客网 时间:2024/05/22 05:09

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

}

0 0
原创粉丝点击