C: If string contains...?
This question already has an answer here:
你可以使用:
if ( result && [result rangeOfString:@"hello"].location != NSNotFound ) {
// Substring found...
}
You have to use - (NSRange)rangeOfString:(NSString *)aString
NSRange range = [myStr rangeOfString:@"hello"];
if (range.location != NSNotFound) {
NSLog (@"Substring found at: %d", range.location);
}
View more here: NSString rangeOfString
If the intent of your code is to check if a string contains another string you can create a category to make this intent clear.
@interface NSString (additions)
- (BOOL)containsString:(NSString *)subString;
@end
@implementation NSString (additions)
- (BOOL)containsString:(NSString *)subString {
BOOL containsString = NO;
NSRange range = [self rangeOfString:subString];
if (range.location != NSNotFound) {
containsString = YES;
}
return containsString;
}
@end
I have not compiled this code, so maybe you should have to change it a bit.
Quentin
链接地址: http://www.djcxy.com/p/73986.html上一篇: IOS:将NSDate对象转换为字符串以获取当前时间
下一篇: C:如果字符串包含...?