0

I would like to test a string to see if anywhere it contains the text "hello". I would like the test to not take into account capitalization. How can I test this string?

Blane Townsend
  • 2,888
  • 5
  • 41
  • 55

5 Answers5

3

-[NSString rangeOfString: options:] will do it.

3

Use the below code as reference to find check for a substring into a string.

    NSString* string = @"How to test a string for text" ;
    NSString* substring  = @"string for" ;

    NSRange textRange;
    textRange =[string rangeOfString:substring  options:NSCaseInsensitiveSearch];

    if(textRange.location != NSNotFound)
    {

    //Does contain the substring
    }
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
1
NSRange range = [string rangeOfString:@"hello" options:NSCaseInsensitiveSearch];
BOOL notFound = range.location==NSNotFound;
Jano
  • 62,815
  • 21
  • 164
  • 192
-1

I am assuming all words are separated by a space, and that there is no punctuation. If there is punctuation.

NSArray *dataArray = [inputString componentsSeparatedByString:@" "];

for(int i=0; i<[dataArray count]){
 if([[dataArray objectAtIndex:i] isEqualToString:@"hello"]){
    NSLog(@"hello has been found!!!");
 } 
}

I haven't tested this but it should work in theory.

Check out the docs for ways to remove punctuation and make the string all lower case. This should be pretty straight-forward.

James
  • 2,272
  • 1
  • 21
  • 31
  • go with what Graham Lee said. Makes way more sense, didn't realize that function existed. – James Apr 24 '11 at 17:30
-2

Other solutions here are good but you should really use a regex,

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^(hello)*$"
                                                                   options:NSRegularExpressionCaseInsensitive
                                                                     error:&error];

Docs are here: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html

sciritai
  • 3,688
  • 1
  • 17
  • 22
  • 3
    Two things: firstly your regex doesn't satisfy the asker's requirement; secondly _why_ should a regex "really" be used? Because perl is cool? I'm not disagreeing, but without justification such a statement is worthless. –  Apr 24 '11 at 17:33
  • Come on, don't be sarcastic. Any kind of matching should be done with a regex. Regular expressions are language agnostic. – sciritai Apr 24 '11 at 17:42
  • 2
    @sciritai but _why_? What does using `NSRegularExpression` get that `NSString`'s built-in searching can't do in an order of magnitude fewer lines? –  Apr 24 '11 at 17:43
  • Harp on all you like about fewer lines, it's good practice. My suggestion is valid. – sciritai Apr 24 '11 at 17:46
  • 6
    Let me put this another way. _Why?_ You say "you should really use a regex", I'm just interested to find out why that's better. So far you haven't been able to provide a reason. If there is a genuine reason why using a regex is better in this situation, I'd be interested to hear it, because I've used NSString's string-searching API without problem since WebObjects 4.5. –  Apr 24 '11 at 17:49
  • It's possible that the asker will want to match multiple "hello" words (rangeOfString only matches the first). He may want to match different variations of hello, such as a misspelled "helo". He may want to make more complex matches for words in the future of his application. You know what scalability means? In general, it's very good to be familiar with regexes because they are far superior than a hack function like your suggestion. If you were genuinely interested, you'd write a little more politely. – sciritai Apr 24 '11 at 18:01