Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search string in Array<AnyObject> with case insensitive

I have application that stores string values from server. Then I am using SearchView for write string that can be in List. It must be case insensitive. So far I have this. But this is not the magic i am looking for.

if (myListTags as NSArray).containsObject(searchBar.text!) {
            print("FOUND")
            getCategoryPick()
}

I tried to join it with caseInsensitiveCompare but i failed. Any help for me? It would be perfect if that code can understand letter similarity in czech language .. (č = c, ž = z, í = i ......)

like image 604
Pan Mluvčí Avatar asked Dec 25 '22 10:12

Pan Mluvčí


1 Answers

"č" is the Unicode character "LATIN SMALL LETTER C WITH CARON". It can be decomposed into "c" + "ˇ" where the latter (the "CARON") is a so-called "diacritical mark".

To ignore diacritical marks when comparing strings (so that "č" = "c"), use the .DiacriticInsensitiveSearch option:

let list = ["č", "ž"]
let search = "C"

if (list.contains {
    $0.compare(search, options: [.DiacriticInsensitiveSearch, .CaseInsensitiveSearch]) == .OrderedSame
    }) {
        print("Found")
} 

Or, if you want to find matching substrings:

if (list.contains {
    $0.rangeOfString(search, options: [.DiacriticInsensitiveSearch, .CaseInsensitiveSearch]) != nil
    }) {
        print("Found")
} 
like image 99
Martin R Avatar answered Jan 12 '23 12:01

Martin R