views:

64

answers:

1

How can I detect if a string contains a certain word? For example, I have a string below which reads:

@"Here is my string."

I'd like to know if I can detect a word in the string, such as, "is" for example.

THank you in advance

+5  A: 

Here's how I would do it:

NSString *someString = @"Here is my string";
NSRange range = [someString rangeOfString:@" is " options:NSCaseInsensitiveSearch];
if( range.location != NSNotFound ) {
   //found it...
}
Jacob Relkin
NSNotFound is not 0... corrected code.
Kendall Helmstetter Gelner
indeed, and also there is `options: NSCaseInsensitiveSearch` which is probably self explanatory.
Cole
@Kendall, you're right. Thanks!
Jacob Relkin
That will also match `@"This string"`, which might not be what he wants.
Wevah
@Wevah, thanks! I updated my answer.
Jacob Relkin