Is there any use case, where String.lowercased().contains will behave differently than String.localizedCaseInsensitiveContains?

Viewed 44

I try to test the following code under different Locale settings

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

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

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

However, all of them behave same under different system Locale settings.

I was wondering, is there any good example, where where String.lowercased().contains will behave differently than String.localizedCaseInsensitiveContains?

Or, those 2 code are just equivalent?

1 Answers

Here is an example that I could observe a different behavior from the language Turkish.

In below code, text5.localizedCaseInsensitiveContains("Şemsİye") returns false, but comparing it to text6.localizedCaseInsensitiveContains("çarŞamba") it should have returned true.

let text5 = "Siyah şemsiye."
let text6 = "Çarşamba çarşafa dolanır."

text5.lowercased().contains("şemsiye".lowercased()) // true
text5.lowercased().contains("şemsİye".lowercased()) // false

text6.lowercased().contains("çarşamba".lowercased()) // true
text6.lowercased().contains("Çarsamba".lowercased()) // false

text5.localizedCaseInsensitiveContains("şemsiye") // true
text5.localizedCaseInsensitiveContains("Şemsiye") // true
text5.localizedCaseInsensitiveContains("Şemsİye") // false

text6.localizedCaseInsensitiveContains("çarşamba") // true
text6.localizedCaseInsensitiveContains("Çarşamba") // true
text6.localizedCaseInsensitiveContains("çarŞamba") // true

The character "İ" is the Turkish capital of "i". The difference seems unique to this character because "ö", "ü" seems to be working like the character "ç".

let i = "i"
let ı = "ı"
let İ = "İ"
let I = "I"

i.capitalized // "I"
ı.capitalized // "I"
İ.lowercased() // "i̇"
İ.localizedLowercase // "i̇" (it has two dots, but in Turkish it should be "i" with one dot.
I.lowercased() // "i"
Related