elect
11/30/2022, 3:05 PMcontains on List<String> with caseInsensitive: Boolean?Chris Lee
11/30/2022, 3:07 PMlist.any { it.lowerCase() == value }elect
11/30/2022, 3:07 PMChris Lee
11/30/2022, 3:08 PMephemient
11/30/2022, 3:09 PMephemient
11/30/2022, 3:11 PMcontains on a (case-folded) Set<String>CLOVIS
11/30/2022, 3:13 PMlist.any { it.equals(value, ignoreCase = true) }
is probably faster / uses less memoryKlitos Kyriacou
11/30/2022, 5:07 PMephemient
11/30/2022, 5:14 PMequals(ignoreCase = true) does not handle some non-English characters well (most notably Turkic languages)ephemient
11/30/2022, 5:25 PMimport java.text.Collator
import java.util.Locale
Collator.getInstance(Locale("tr")).apply { strength = Collator.SECONDARY }.compare("I", "i") // -1, because these are not case-insensitive equal in Turkish
Collator.getInstance(Locale("tr")).apply { strength = Collator.SECONDARY }.compare("İ", "i") // 0, because these are case-insensitive equal in Turkish
Collator.getInstance(Locale("de")).apply { strength = Collator.SECONDARY }.compare("SS", "ß") // 0, because these are case-insensitive equal in German
equals(ignoreCase = true) has no way to get any of these cases right. you should really consider it as an ASCII-only APIelect
11/30/2022, 5:26 PMephemient
11/30/2022, 5:44 PMCollator.getCollationKey, which will be more efficient than doing case-insensitive comparisons repeatedly. for example,
val collationKeys = buildSet { listOfStrings.mapTo(this) { collator.getCollationKey(it) } }
collator.getCollationKey(string1) in collationKeys
collator.getCollationKey(string2) in collationKeys
// etc.elect
12/01/2022, 9:20 AM