-3

What method of NSString checks if a string found by a localizedCaseInsensitiveCompare: keyword is contained in another?

NSString *listOfNames = @"anas, ward, qusai, zainab";
NSString *keyword = @"Ward";

if ([listOfNames localizedCaseInsensitiveCompare:keyword]) {
    NSLog(@"\nMatch found!\n");
} else {
    NSLog(@"\nNo match found!\n");
}
Shakespear
  • 1,280
  • 3
  • 19
  • 25
  • I cannot relate your question to the code. Please clarify your question. – Eiko Jun 23 '16 at 17:56
  • @Eiko how to return actual string found from `listOfNames`? here `"Ward"`..with capital W. – Shakespear Jun 24 '16 at 06:50
  • Use one of the rangeOfString: methods with the right options as @Josh Caswell says. Given the range, you can then easily get the substring. – Eiko Jun 24 '16 at 06:52

2 Answers2

1

The localizedCaseInsensitiveCompare most likely internally converts both strings to lowercase and then does a compare using a localized collation algorithm.

From the docs: "Localized string comparisons are based on the Unicode Collation Algorithm, as tailored for different languages by CLDR (Common Locale Data Repository). Both are projects of the Unicode Consortium. Unicode is a registered trademark of Unicode, Inc."

Probably a reasonable thing to do is convert each string to lowercase, using something like listOfNames.lowercaseString

seand
  • 5,044
  • 1
  • 21
  • 35
  • thanks, but what i meant...the output here is `// Match found!` when i search for ward (lowercase)...but what i need more, is to get `// Match found! Ward ..!`...Ward here is the actual value in the string. – Shakespear Jun 23 '16 at 17:55
  • Perhaps do a regex match? – seand Jun 23 '16 at 17:58
1

I assume that you mean you're using localizedCaseInsensitiveContainsString: since the method you've cited doesn't do what you're saying.

There's a corresponding method to get the range of the search term: -[NSString localizedStandardRangeOfString:] You use the returned range to index back into the source string.

You can also use the rangeOfString:options:range:locale: if you need to search with a locale other than the current one.

jscs
  • 62,161
  • 12
  • 145
  • 186