String.equalFold
func String.equalFold(a: string, b: string): boolString.equalFold reports whether a and b are equal without regard to case, as String.foldCase(a) == String.foldCase(b).
Details
String.equalFold(a, b)isString.foldCase(a) == String.foldCase(b).- Full case folding maps one character to several where Unicode says so:
"Straße"and"STRASSE"are equal. - There is no Turkish rule:
"İ"and"i"are not equal, nor are"ı"and"I". - It changes case only. It does not remove accents, so
"café"and"CAFE"differ, and it does not normalize; useString.normalize(s, "NFC")on both first. - The
iflag of a regular expression folds one code point at a time, so/straße/idoes not match"STRASSE".
Basic examples
Compare two words without regard to case:
String.equalFold("Ant", "ANT") // true
String.equalFold("Ant", "ants") // falseCall it through . on the first string:
"Hello".equalFold("hELLO") // trueScope
Two empty strings are equal, and an empty string equals no other:
"".equalFold("") // true
"".equalFold(" ") // falseCase folding can change the length of a string. "ß" folds to "ss":
"Straße".equalFold("STRASSE") // true
"ß".equalFold("SS") // trueIt covers every script with case, such as Greek, where the final sigma ς and σ fold alike:
"σοφος".equalFold("ΣΟΦΟΣ") // true
"Ångström".equalFold("ÅNGSTRÖM") // trueCompatibility characters that have case fold too. The ligature fi folds to "fi", and the Kelvin sign K to "k":
"fi".equalFold("FI") // true
"\u{212A}".equalFold("k") // trueCharacters without case, such as digits, spaces, and punctuation, must match exactly:
"Room 101!".equalFold("ROOM 101!") // true
"Room 101".equalFold("room 101") // falseIn a pipe, the left side is a:
"README.md" | String.equalFold("readme.MD") // trueName the arguments:
String.equalFold(a: "Yes", b: "YES") // trueApplications
Accept a yes or no answer however the user typed it:
func confirmed(answer: string): bool {
let a = answer.trim()
a.equalFold("y") || a.equalFold("yes")
}
confirmed(" Yes ") // true
confirmed("YES") // true
confirmed("no") // falseLook up an HTTP header, whose names are case-insensitive:
type Header = { name: string, value: string }
let headers = [
Header{ name: "Content-Type", value: "text/html" },
Header{ name: "X-Request-Id", value: "42" },
]
headers.find({ x.name.equalFold("content-type") })?.value // "text/html"Check a file extension without regard to case:
func isImage(name: string): bool {
let dot = name.lastIndexOf(".")
if dot == null {
return false
}
let ext = name.slice(dot + 1, name.length())
["png", "jpg", "gif"].some({ ext.equalFold(x) })
}
isImage("photo.JPG") // true
isImage("notes.txt") // false
isImage("README") // falseProperties and relations
equalFold is reflexive and symmetric:
let a = "Straße"
let b = "STRASSE"
assert(a.equalFold(a))
assert(a.equalFold(b) == b.equalFold(a))It agrees with comparing the folded strings with ==:
let pairs = [["Ant", "aNT"], ["ß", "ss"], ["İ", "i"], ["a", "b"]]
for p in pairs {
assert(p[0].equalFold(p[1]) == (p[0].foldCase() == p[1].foldCase()))
}Possible issues
Comparing with String.toLower is not the same. Lower case keeps ß, so it misses what folding finds:
"Straße".toLower() == "STRASSE".toLower() // false
"Straße".equalFold("STRASSE") // trueequalFold does not normalize. The same accented letter spelled two ways compares false until both are normalized:
let composed = "\u{E9}"
let decomposed = "e\u{301}"
composed.equalFold(decomposed) // false
let nfc = decomposed.normalize("NFC")
composed.equalFold(nfc) // trueThe Turkish dotted and dotless i do not fold to i and I. The full case folding has no Turkish rule, so "İ" folds to "i\u{307}" and "ı" stays "ı":
"İ".equalFold("i") // false
"ı".equalFold("I") // falseFolding changes case only, not accents, so é does not match E:
"café".equalFold("CAFE") // falseTo ignore accents as well, strip them first: split each accent from its letter with String.normalize in NFD, then drop the code points that String.isMark accepts:
func stripAccents(s: string): string {
s.normalize("NFD")
| String.chars()
| Array.filter({ !x.isMark() })
| String.join("")
}
stripAccents("café").equalFold("CAFE") // trueA regex with the i flag folds one code point at a time, so it can reject what equalFold accepts:
Regex.test(/straße/i, "STRASSE") // false
"straße".equalFold("STRASSE") // trueTo group or deduplicate strings without regard to case, use String.foldCase as the key instead of comparing every pair with equalFold:
["Ant", "ANT", "bee", "Bee"].uniqueBy(String.foldCase) // ["Ant", "bee"]See also
String.foldCase · String.toLower · String.toUpper · String.normalize · Regex.test · Array.uniqueBy
