ios - 在Objective-C如何检查字符串是否包含另一个字符串 ?

  显示原文与译文双语对照的内容

如何检查字符串( NSString ) 是否包含另一个较小的字符串?

我想要的是:


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

但我最接近的是:


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

无论如何,如果一个字符串包含另一个字符串,那是最好的方法?

时间:


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

关键是注意到 rangeOfString: 返回一个 NSRange 结构、和文档说返回结构体 {NSNotFound, 0} 如果"haystack"不包含"唱针"。


如果你在 iOS 8或者 OS X Yosemite上,你现在可以:


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

在NSString上创建一个类别:


@interface NSString ( containsCategory )
- (BOOL) containsString: (NSString*) substring;
@end

//- - - - 

@implementation NSString ( containsCategory )

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

@end

编辑:观察丹尼尔的评论galasko下面关于命名
磅编辑:注意这个答案现在已经过时了


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

}

//You 也可以另外使用:


if (rangeValue.location == NSNotFound) {

NSLog(@"string does not contain hello");

} 

else {

NSLog(@"string contains hello!");

}

因为这似乎是谷歌的高级结果,我想添加:

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


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

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


 let string: NSString ="Café"
 let substring: NSString ="É"

 string.localizedCaseInsensitiveContainsString(substring)//true

下面是一个copy-and-paste函数 Fragment:


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

一个改进版的P i 解决方案,NSString范畴,不仅会告诉,如果找到一个字符串在另一个字符串,但也需要参考的范围,是:


@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));
}

Oneliner ( 少量代码) 。 DRY,因为你只有一个 NSLog ):


NSString *string = @"hello bla bla";
NSLog(@"String %@", ([string rangeOfString:@"bla"].location == NSNotFound)? @"not found" : @"cotains bla"); 

既然你正在寻找最佳的方法,这里是:


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

在本例中,你知道字符串是"喂,bla"。 但是如果字符串也是零,那么在检查位置时就有问题。 因为在零字符串上的位置是 0,所以你无法检查!= NSNotFound 。 所以,使用长度> 0它更安全。

如果你需要一次写入:


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;

...