4

I have some string value which constructed from a few characters , and i want to check if they exist in another NSString, without case sensitive, and spaces .

Example code :

NSString *me = @"toBe" ;
NSString *target=@"abcdetoBe" ;
//than check if me is in target.

Here i will get true because me exist in target . How can i check for such condition ?

I have read How do I check if a string contains another string in Objective-C? but its case sensitive and i need to find with no case sensitive..

Community
  • 1
  • 1
Curnelious
  • 1
  • 10
  • 65
  • 133
  • 2
    possible duplicate of http://stackoverflow.com/q/5739071/148357 – Antonio Feb 23 '14 at 14:03
  • Have a look at NSScanner class [link](https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSScanner_Class/Reference/Reference.html#//apple_ref/doc/uid/20000159-scanString_intoString_). You can set the scanner to be case insensitive and then use the scanString:intoString: method. – Bamsworld Feb 23 '14 at 14:03
  • In the link you provided there is an answer for case insensitive search `-rangeOfStringoptions:NSCaseInsensitiveSearch` – Emmanuel Feb 23 '14 at 14:08
  • 1
    possible duplicate of [How do I check if a string contains another string in Objective-C?](http://stackoverflow.com/questions/2753956/how-do-i-check-if-a-string-contains-another-string-in-objective-c) – Emmanuel Feb 23 '14 at 14:09

3 Answers3

20

Use the option NSCaseInsensitiveSearch with rangeOfString:options:

NSString *me = @"toBe" ;
NSString *target = @"abcdetobe" ;
NSRange range = [target  rangeOfString: me options: NSCaseInsensitiveSearch];
NSLog(@"found: %@", (range.location != NSNotFound) ? @"Yes" : @"No");
if (range.location != NSNotFound) {
    // your code
}

NSLog output:

found: Yes

Note: I changed the target to demonstrate that case insensitive search works.

The options can be "or'ed" together and include:

  • NSCaseInsensitiveSearch
  • NSLiteralSearch
  • NSBackwardsSearch
  • NSAnchoredSearch
  • NSNumericSearch
  • NSDiacriticInsensitiveSearch
  • NSWidthInsensitiveSearch
  • NSForcedOrderingSearch
  • NSRegularExpressionSearch
zaph
  • 108,117
  • 19
  • 176
  • 215
2
-(BOOL)substring:(NSString *)substr existsInString:(NSString *)str {
    if(!([str rangeOfString:substr options:NSCaseInsensitiveSearch].length==0)) {
        return YES;
    }

    return NO;
}

usage:

NSString *me = @"toBe";
NSString *target=@"abcdetoBe";
if([self substring:me existsInString:target]) {
    NSLog(@"It exists!");
}
else {
    NSLog(@"It does not exist!");
}
zaph
  • 108,117
  • 19
  • 176
  • 215
Jonathan Gurebo
  • 1,049
  • 1
  • 11
  • 21
  • The method fails for `target = @"toBeabcdetobe"` and `target = @"abcdetob"`, you need to compare against `NSNotFound`. Also the code fails to compile. Hint: Get "CodeRunner" (from AppStore) to verify code. – zaph Feb 23 '14 at 15:18
  • I changed it to length – Jonathan Gurebo Feb 23 '14 at 19:58
  • I have used the code before.... I also use the bool (not BOOL) object all the time- – Jonathan Gurebo Feb 23 '14 at 21:39
  • The Apple docs for rangeOfString:options: state: "Returns {NSNotFound, 0} if aString is not found or is empty (@"")." Sure length is working but it is best practice to use the convention that is documented and well understood. Also bool will work but the convention for Objective-C is BOOL. From this [SO answer](http://stackoverflow.com/a/544250/451475): You can use the (C99) bool type, but all of Apple's Objective-C frameworks and most Objective-C/Cocoa code uses BOOL, so you'll save yourself headache if the typedef ever changes by just using BOOL. – zaph Feb 23 '14 at 22:33
  • ok, I have never really understand the bool vs BOOL. Im going to use BOOL in the future. Thanks for helping – Jonathan Gurebo Feb 23 '14 at 22:36
1

As with the release of iOS8, Apple added a new method to NSStringcalled localizedCaseInsensitiveContainsString. This will exactly do what you want:

Swift:

let string: NSString = "ToSearchFor"
let substring: NSString = "earch"

string.localizedCaseInsensitiveContainsString(substring) // true

Objective-C:

NSString *string = @"ToSearchFor";
NSString *substring = @"earch";

[string localizedCaseInsensitiveContainsString:substring]; //true
Alexander
  • 7,028
  • 8
  • 42
  • 73