What is the best way to compare two NSString objects, ignoring case?

I want to compare two lines. It does not work when the string is uppercase. How to convert both strings to capital letters and compare.

I have some sample code, can someone fix this.

if ([[txtAnswer.text capitalizedString] isEqualToString:[answer capitalizedString]]) { // Do somehing } 
+4
source share
3 answers

If you look at the link of the NSString class , you will see the caseInsensitiveCompare: and localizedCaseInsensitiveCompare: methods under the "Identification and String Comparison" caseInsensitiveCompare: .

+8
source

You can try something like:

 if ([txtAnswer.text caseInsensitiveCompare: answer] == NSOrderedSame) { // do something. } 
+6
source

You can do case-insensitive string comparisons.

 if([txtAnswer.text compare:answer options:NSCaseInsensitiveSearch] == NSOrderedSame) { // Do somehing } 
+3
source

Source: https://habr.com/ru/post/1379552/


All Articles