ios - rangeofstring - swift string contain string




如何檢查一個字符串是否包含Objective-C中的另一個字符串? (13)

我怎樣才能檢查一個字符串( 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 *first = @"Banana";
    NSString *second = @"BananaMilk";
    NSRange range = [first rangeOfString:second options:NSCaseInsensitiveSearch];

    if (range.length > 0) {
        NSLog(@"Detected");
    }
    else {
        NSLog(@"Not detected");
    }

Oneliner(更少量的代碼,DRY,因為你只有一個NSLog ):

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

注意:此答案現在已過時

為NSString創建一個類別:

@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 8Swift ,我們可以使用localizedCaseInsensitiveContainsString方法

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

 string.localizedCaseInsensitiveContainsString(substring) // true

在Swift 4中:

let a = "Hello, how are you?"
a.contains("Hello")   //will return true

在迅速的情況下,這可以使用

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

如果你需要這樣寫一次:

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;

如果需要某個字符串的位置,這段代碼會在Swift 3.0中出現

let string = "This is my string"
let substring = "my"

let position = string.range(of: substring)?.lowerBound

最佳解決方案 像這樣簡單! 如果你想找到一個字或字符串的一部分。 您可以使用此代碼。 在這個例子中,我們將檢查單詞的值是否包含“acter”。

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

由於這似乎是Google的高級結果,我想補充一點:

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

這是一個複制和粘貼功能片段:

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

NSString *categoryString = @"Holiday Event";
if([categoryString rangeOfString:@"Holiday"].location == NSNotFound)
{
    //categoryString does not contains Holiday
}
else
{
    //categoryString contains Holiday
}

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

關鍵是注意到rangeOfString:返回一個NSRange結構,並且文檔說如果“haystack”不包含“needle”,它將返回結構{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");
}

👍





substring