如何检查一个string是否包含Objective-C中的另一个string?

我怎样才能检查一个string( NSString )是否包含另一个更小的string?

我希望有这样的东西:

 NSString *string = @"hello bla bla"; NSLog(@"%d",[string containsSubstring:@"hello"]); 

但最接近我能find的是:

 if ([string rangeOfString:@"hello"] == 0) { NSLog(@"sub string doesnt exist"); } else { NSLog(@"exists"); } 

无论如何,是否最好的方法来查找一个string是否包含另一个string?

 NSString *string = @"hello bla bla"; if ([string rangeOfString:@"bla"].location == NSNotFound) { NSLog(@"string does not contain bla"); } else { NSLog(@"string contains bla!"); } 

关键是注意到rangeOfString:返回一个NSRange结构, 文档说如果“干草堆”不包含“needle”,它将返回struct {NSNotFound, 0}


如果您使用的是iOS 8或OS X Yosemite,则可以执行以下操作: (*注意:如果在iOS7设备上调用此代码,将会使您的应用程序崩溃)。

 NSString *string = @"hello bla blah"; if ([string containsString:@"bla"]) { NSLog(@"string contains bla!"); } else { NSLog(@"string does not contain bla"); } 

👍

注意:这个答案现在已经过时了

为NSString创build一个类别:

 @interface NSString ( SubstringSearch ) - (BOOL)containsString:(NSString *)substring; @end // - - - - @implementation NSString ( SubstringSearch ) - (BOOL)containsString:(NSString *)substring { NSRange range = [self rangeOfString : substring]; BOOL found = ( range.location != NSNotFound ); return found; } @end 

编辑:观察下面有关命名的Daniel Galasko的评论

由于这似乎是在谷歌的高排名结果,我想补充一点:

iOS 8和OS X 10.10将containsString:方法添加到NSString 。 Dave DeLong对这些系统的更新版本:

 NSString *string = @"hello bla bla"; if ([string containsString:@"bla"]) { NSLog(@"string contains bla!"); } else { NSLog(@"string does not contain bla"); } 
 NSString *myString = @"hello bla bla"; NSRange rangeValue = [myString rangeOfString:@"hello" options:NSCaseInsensitiveSearch]; if (rangeValue.length > 0) { NSLog(@"string contains hello"); } else { NSLog(@"string does not contain hello!"); } 

//您也可以使用以下内容:

 if (rangeValue.location == NSNotFound) { NSLog(@"string does not contain hello"); } else { NSLog(@"string contains hello!"); } 

使用iOS 8和Swift,我们可以使用localizedCaseInsensitiveContainsString方法

  let string: NSString = "Café" let substring: NSString = "É" string.localizedCaseInsensitiveContainsString(substring) // true 

所以我个人非常讨厌NSNotFound但了解它的必要性。

但有些人可能不了解与NSNotFound比较的复杂性

例如,这个代码:

 - (BOOL)doesString:(NSString*)string containString:(NSString*)otherString { if([string rangeOfString:otherString].location != NSNotFound) return YES; else return NO; } 

有它的问题:

1)显然,如果otherString = nil这个代码将崩溃。 一个简单的testing将是:

 NSLog(@"does string contain string - %@", [self doesString:@"hey" containString:nil] ? @"YES": @"NO"); 

结果是 !! 破坏!

2)对于新的objective-c来说,不那么明显的是,当string = nil时,相同的代码不会崩溃。 例如,这个代码:

 NSLog(@"does string contain string - %@", [self doesString:nil containString:@"hey"] ? @"YES": @"NO"); 

和这个代码:

 NSLog(@"does string contain string - %@", [self doesString:nil containString:nil] ? @"YES": @"NO"); 

都会导致

 does string contains string - YES 

这显然不是你想要的。

所以我认为更好的解决scheme是使用rangeOfString返回0的长度这样一个事实,那么更好的可靠代码是这样的:

 - (BOOL)doesString:(NSString*)string containString:(NSString*)otherString { if(otherString && [string rangeOfString:otherString].length) return YES; else return NO; } 

或简单:

 - (BOOL)doesString:(NSString*)string containString:(NSString*)otherString { return (otherString && [string rangeOfString:otherString].length); } 

对于案例1和2将返回

 does string contains string - NO 

这是我的2美分;-)

请查看我的要点以获取更多有用的代码。

P i解决scheme的一个改进版本,它是NSString上的一个类别,它不仅会告诉,如果在另一个string中find一个string,而且还通过引用取得一个范围,则:

 @interface NSString (Contains) -(BOOL)containsString: (NSString*)substring atRange:(NSRange*)range; -(BOOL)containsString:(NSString *)substring; @end @implementation NSString (Contains) -(BOOL)containsString:(NSString *)substring atRange:(NSRange *)range{ NSRange r = [self rangeOfString : substring]; BOOL found = ( r.location != NSNotFound ); if (range != NULL) *range = r; return found; } -(BOOL)containsString:(NSString *)substring { return [self containsString:substring atRange:NULL]; } @end 

像这样使用它:

 NSString *string = @"Hello, World!"; //If you only want to ensure a string contains a certain substring if ([string containsString:@"ello" atRange:NULL]) { NSLog(@"YES"); } // Or simply if ([string containsString:@"ello"]) { NSLog(@"YES"); } //If you also want to know substring's range NSRange range; if ([string containsString:@"ello" atRange:&range]) { NSLog(@"%@", NSStringFromRange(range)); } 

这是一个复制和粘贴function代码片段:

 -(BOOL)Contains:(NSString *)StrSearchTerm on:(NSString *)StrText { return [StrText rangeOfString:StrSearchTerm options:NSCaseInsensitiveSearch].location != NSNotFound; } 

Oneliner(更less量的代码,DRY,因为你只有一个NSLog ):

 NSString *string = @"hello bla bla"; NSLog(@"String %@", ([string rangeOfString:@"bla"].location == NSNotFound) ? @"not found" : @"cotains bla"); 
 NSString *categoryString = @"Holiday Event"; if([categoryString rangeOfString:@"Holiday"].location == NSNotFound) { //categoryString does not contains Holiday } else { //categoryString contains Holiday } 

尝试这个,

 NSString *string = @"test Data"; if ([[string lowercaseString] rangeOfString:@"data"].location == NSNotFound) { NSLog(@"string does not contain Data"); } else { NSLog(@"string contains data!"); } 

如果你需要这个一次写:

 NSString *stringToSearchThrough = @"-rangeOfString method finds and returns the range of the first occurrence of a given string within the receiver."; BOOL contains = [stringToSearchThrough rangeOfString:@"occurence of a given string"].location != NSNotFound; 

在迅速的情况下,这可以使用

 let string = "Package #23" if string.containsString("Package #") { //String contains substring } else { //String does not contain substring } 

最佳解决scheme 像这样简单! 如果你想find一个单词或string的一部分。 你可以使用这个代码。 在这个例子中,我们将检查单词的值是否包含“acter”。

 NSString *word =@"find a word or character here"; if ([word containsString:@"acter"]){ NSLog(@"It contains acter"); } else { NSLog (@"It does not contain acter"); } 

如果不打扰区分大小写的string。 试试这一次。

 NSString *string = @"Hello World!"; if([string rangeOfString:@"hello" options:NSCaseInsensitiveSearch].location !=NSNotFound) { NSLog(@"found"); } else { NSLog(@"not found"); } 

请使用此代码

 NSString *string = @"hello bla bla"; if ([string rangeOfString:@"bla"].location == NSNotFound) { NSLog(@"string does not contain bla"); } else { NSLog(@"string contains bla!"); } 

在斯威夫特4

让a =“你好,你好吗?”

a.contains(“Hello”)// true

如果需要某个位置的string,这个代码来Swift 3.0

 let string = "This is my string" let substring = "my" let position = string.range(of: substring)?.lowerBound