Scott
Scott

Reputation: 783

Detect whole word in NSStrings

How do I detect if an NSString contains a specific word, e.g. is.

If the NSString is Here is my string. His isn't a mississippi isthmus. It is...? The method should detect the word is and return YES.

However, if the NSString is His isn't a mississipi isthmus, it should return NO.

I tried using if ([text rangeOfString:@"is" options:NSCaseInsensitiveSearch].location != NSNotFound) { ... } but it detects characters not words.

Upvotes: 6

Views: 2107

Answers (4)

Martin R
Martin R

Reputation: 539685

Use "regular expression" search with the "word boundary pattern" \b:

NSString *text = @"Here is my string. His isn't a mississippi isthmus. It is...";
NSString *pattern = @"\\bis\\b";
NSRange range = [text rangeOfString:pattern options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
if (range.location != NSNotFound) { ... }

This works also for cases like "Is it?" or "It is!", where the word is not surrounded by spaces.

In Swift 2 this would be

let text = "Here is my string. His isn't a mississippi isthmus. It is..."
let pattern = "\\bis\\b"
if let range = text.rangeOfString(pattern, options: [.RegularExpressionSearch, .CaseInsensitiveSearch]) {
    print ("found:", text.substringWithRange(range))
}

Swift 3:

let text = "Here is my string. His isn't a mississippi isthmus. It is..."
let pattern = "\\bis\\b"
if let range = text.range(of: pattern, options: [.regularExpression, .caseInsensitive]) {
    print ("found:", text.substring(with: range))
}

Swift 4:

let text = "Here is my string. His isn't a mississippi isthmus. It is..."
let pattern = "\\bis\\b"
if let range = text.range(of: pattern, options: [.regularExpression, .caseInsensitive]) {
    print ("found:", text[range])
}

Swift 5 (using the new raw string literals):

let text = "Here is my string. His isn't a mississippi isthmus. It is..."
let pattern = #"\bis\b"#
if let range = text.range(of: pattern, options: [.regularExpression, .caseInsensitive]) {
    print ("found:", text[range])
}

Upvotes: 16

Guillaume
Guillaume

Reputation: 4361

You could use regular expressions, as suggested, or you could analyze the words linguistically:

NSString *string = @"Here is my string. His isn't a mississippi isthmus. It is...";
__block BOOL containsIs = NO;
[string enumerateLinguisticTagsInRange:NSMakeRange(0, [string length]) scheme:NSLinguisticTagSchemeTokenType options:NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitOther orthography:nil usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop){
    NSString *substring = [string substringWithRange:tokenRange];
    if (containsIs)
        if ([substring isEqualToString:@"n't"])
            containsIs = NO; // special case because "isn't" are actually two separate words
        else
            *stop = YES;
    else
        containsIs = [substring isEqualToString:@"is"];
}];
NSLog(@"'%@' contains 'is': %@", string, containsIs ? @"YES" : @"NO");

Upvotes: 0

Michał Ciuba
Michał Ciuba

Reputation: 7944

Use NSRegularExpressionSearch option with \b to match word boundary characters.

Like this:

NSString *string = @"Here is my string. His isn't a mississippi isthmus. It is...";
if(NSNotFound != [string rangeOfString:@"\\bis\\b" options:NSRegularExpressionSearch].location) {//...}

Upvotes: 1

Antonio MG
Antonio MG

Reputation: 20410

What about

if ([text rangeOfString:@" is " options:NSCaseInsensitiveSearch].location != NSNotFound) { ... }

Upvotes: 0

Related Questions