iOS 判断一个字符串是否包含另外一个字符串
(文章转载自:http://www.guoms.com/?p=99)
iOS 判断一个字符串是否包含另外一个字符串
判断一个字符串是否包含另外一个字符串,我们可以判断某个字符串在另外一个字符串是否有location如果有证明存在,如果没有证明不存在。在iOS8中我们可以直接判断两个字符是否包含:
|
1
2
3
4
5
6
7
|
//判断字符串的位置
NSString *string = @"hello moon";
if ([string rangeOfString:@"moon"].location == NSNotFound) {
NSLog(@"string 不存在 moon");
} else {
NSLog(@"string 包含 moon");
}
|
|
1
2
3
4
5
6
7
|
//在iOS8中你可以这样判断
NSString *str = @"hello world";
if ([str containsString:@"world"]) {
NSLog(@"str 包含 world");
} else {
NSLog(@"str 不存在 world");
}
|
浙公网安备 33010602011771号