36

In Swift 3, I want to create an array of matching string (case insensitive) from string array:-

I am using this code, but it is case sensitive,

let filteredArray = self.arrCountry.filter { $0.contains("india") }

how can I do this.. suppose I have a master string array called arrCountry, I want to create other array of all the string who has "india"(case insensitive) in it.

Can anyone help me out.

Mayank Jain
  • 5,663
  • 7
  • 32
  • 65

6 Answers6

102

You can try with localizedCaseInsensitiveContains

let filteredArray = self.arrCountry.filter { $0.localizedCaseInsensitiveContains("india") }
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
16

localizedCaseInsensitiveContains
Returns a Boolean value indicating whether the given string is non-empty and contained within this string by case-insensitive, non-literal search, taking into account the current locale. Locale-independent case-insensitive operation, and other needs, can be achieved by calling range(of:options:range:locale:).

Equivalent to: range(of: other, options: .caseInsensitiveSearch, locale: Locale.current) != nil

It's better to use

.filter { $0.range(of: "india", options: .caseInsensitive) != nil }
Nike Kov
  • 12,630
  • 8
  • 75
  • 122
  • 1
    why is it better? your version seems more verbose; the other, more streamlined. – Zonker.in.Geneva Aug 24 '19 at 22:49
  • 1
    @Zonker.in.Geneva because localized version uses the current Locale, but there can be cases, when the Locale is different then the language of the app. – Nike Kov Aug 25 '19 at 23:20
4

The easiest way might be to lowercase your strings and compare:

Swift 3 and above

let filteredArray = self.arrCountry.filter { $0.lowercased() == "india" }
Jonathan Cabrera
  • 1,656
  • 1
  • 19
  • 20
3

Answer 2020:

Even I put Chinese or Arabic, below test codes still returns TRUE

let text = "total sdfs"
let text1 = "Total 张"
let text2 = "TOTAL لطيف"
let text3 = "total :"
let text4 = "ToTaL : "



text.lowercased().contains("total")
text1.lowercased().contains("张")
text2.lowercased().contains("لطيف")
text3.lowercased().contains("total")
text4.lowercased().contains("total")


text.localizedCaseInsensitiveContains("total")
text1.localizedCaseInsensitiveContains("张")
text2.localizedCaseInsensitiveContains("لطيف")
text3.localizedCaseInsensitiveContains("total")
text4.localizedCaseInsensitiveContains("total")
Sky
  • 2,212
  • 3
  • 14
  • 22
0

my 2 cents when using filtering (for example for lists in SwiftUI. if You are filtering, empty string gives back false:

let filterString = ""
let s = "HELLO"
let f = s.localizedCaseInsensitiveContains(filterString)
print(f)

so for example:

....

    private let people = ["Finn", "Leia", "Luke", "Rey"]


   func getList(filterString: String)->[String]{
        
        if filterString.isEmpty{
            return people
        }
        
        let result = people.filter({ (p: String) -> Bool in
            
            print(p)
            let f = p.localizedCaseInsensitiveContains(filterString)
            return f
        })
        
        return result
    }
ingconti
  • 10,876
  • 3
  • 61
  • 48
0

you can use this

let someArray = theRecentSavedData.filter({$0.labelToDisplay?.localizedCaseInsensitiveContains(searchBar.text ?? "") ?? false})

in someArray you will get all the string which matches

For example, if inside theRecentSavedData array you have a string like this "Gel", and if you type "gel" it will give you Gel.

Mihir Oza
  • 2,768
  • 3
  • 35
  • 61
Amit Thakur
  • 1,081
  • 12
  • 14