9

I am looking for a way to perform a case insensitive string search within another string in Objective-C. I could find ways to search case sensitive strings, and to compare insensitive case, but not searching + case insensitive.

Examples of the search that I would like to perform:

"john" within "i told JOHN to find me a good search algorithm"

"bad IDEA" within "I think its a really baD idea to post this question"

I prefer to stick to only NSStrings.

Community
  • 1
  • 1
TommyG
  • 4,145
  • 10
  • 42
  • 66

4 Answers4

31
NSRange r = [MyString rangeOfString:@"Boo" options:NSCaseInsensitiveSearch];

Feel free to encapsulate that into a method in a category over NSString, if you do it a lot. Like this:

@interface NSString(MyExtensions)
-(NSRange)rangeOfStringNoCase:(NSString*)s;
@end

@implementation NSString(MyExtensions)
-(NSRange)rangeOfStringNoCase:(NSString*)s
{
    return  [self rangeOfString:s options:NSCaseInsensitiveSearch];
}
@end

Your code might become more readable with this. Then again, less readable for those unfamiliar.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • 3
    That said, your first stop for questions like this should still be Xcode's online help rather than StackOverflow. Menu, Help/Search for NSString, choose "NSString class reference". – Seva Alekseyev Nov 02 '11 at 22:28
9

mistagged as c#?

heres some advice for objc

NSRange textRange = [[string lowercaseString] rangeOfString:[substring lowercaseString]];

if(textRange.location != NSNotFound)
{
//Does contain the substring
}

which i got from google at this webpage: http://www.developers-life.com/does-a-nsstring-contain-a-substring.html

does that help your scenario?

Mark
  • 509
  • 3
  • 9
  • 1
    Let me check, and thanks for the correction, yes def ObjC :) BTW - why are you using "lowercaseString" for case insensitive? thanks again. – TommyG Nov 02 '11 at 22:10
  • no particular reason - i just copied the sample over from the website as i'm not at my xcode-ing machine at the moment. – Mark Nov 02 '11 at 22:19
  • bottom example is more simple, but get my upvote for great solution nonetheless. thanks! – TommyG Nov 02 '11 at 22:21
  • That's horribly, horribly inefficient, and not guaranteed to work correctly. Your comparison creates two autoreleased strings, converting both to lowercase, which is very inefficient. If string is "a very very long string starting with a ... and so on and so on". and substring is "a" or "very", then this is even more inefficient, because a gazillion of characters are converted with no need. "Doesn't work" because there are interesting characters in Unicode. – gnasher729 Jun 10 '15 at 13:08
8

If you're using ios 8, you can use NSString's localizedCaseInsensitiveContainsString

- (BOOL)localizedCaseInsensitiveContainsString:(NSString *)aString

localizedCaseInsensitiveContainsString: is the case-insensitive variant. Note that it takes the current locale into effect as well. Locale-independent case-insensitive operation, and other needs can be achieved by calling rangeOfString:options:range:locale: directly.

Majestic12
  • 216
  • 3
  • 5
0

Here is a self-container solution for Swift:

private func containsKeyword(text: NSString, keyword: String) -> Bool
{
  return text.rangeOfString(keyword, options:NSStringCompareOptions.CaseInsensitiveSearch).location != NSNotFound
}
Zorayr
  • 23,770
  • 8
  • 136
  • 129