Skip to content

String.foldCase ​

ant
func String.foldCase(s: string): string

String.foldCase returns s with Unicode's full case folding applied, a key in which case does not matter. It leaves out the Turkic entries, so İ folds to i and a combining dot, and ı only to itself. It does not normalize; see String.normalize.

Details ​

  • Maps every case variant of a string to one form, so it is the key for grouping or deduplicating text without regard to case.
  • Full case folding can change the length: "ß" folds to "ss".
  • It does not normalize: "\u{E9}" and "e\u{301}" fold to different strings. Normalize with String.normalize(s, "NFC") first.
  • To compare two strings, use String.equalFold.

Basic examples ​

ant
String.foldCase("Straße")       // "strasse"
String.foldCase("STRASSE")      // "strasse"
String.foldCase("Hello, World") // "hello, world"

Scope ​

An empty string folds to an empty string, and characters without case pass through unchanged:

ant
"".foldCase()           // ""
"123 + 4 🐜".foldCase() // "123 + 4 🐜"

Folding can lengthen a string. The ligature fi folds to the two letters fi:

ant
"ß".foldCase()            // "ss"
"file".foldCase()          // "file"
"file".foldCase().length() // 4

Letters of other scripts fold too. Every Greek sigma, final or not, folds to σ, and the Kelvin sign K folds to the letter k:

ant
"ΣΟΦΟΣ".foldCase()    // "σοφοσ"
"σοφος".foldCase()    // "σοφοσ"
"\u{212A}".foldCase() // "k"

Call it through . or in a pipe:

ant
"Ant".foldCase()          // "ant"
"ANT" | String.foldCase() // "ant"

Pass it as a function value, where a function from string to string is expected:

ant
["Ant", "BEE", "wasp"].map(String.foldCase) // ["ant", "bee", "wasp"]

Applications ​

Remove duplicates without regard to case, keeping the first spelling:

ant
let tags = ["Go", "rust", "GO", "Rust", "ant"]
tags.uniqueBy(String.foldCase) // ["Go", "rust", "ant"]

Group words by their folded form:

ant
let words = ["Ant", "ant", "ANT", "Bee"]
words.groupBy(String.foldCase) // Map{ "ant": ["Ant", "ant", "ANT"], "bee": ["Bee"] }

Look up a map whose keys are folded, so that any spelling finds the entry:

ant
let colors = Map{ "red": "#f00", "green": "#0f0" }
colors.get("RED".foldCase())   // "#f00"
colors.get("Green".foldCase()) // "#0f0"

Count the words of a text without regard to case:

ant
let text = "The ant and THE bee and the Wasp"
let counts: Map[string, int] = Map{}
for w in text.fields() {
  let key = w.foldCase()
  counts[key] = (counts[key] ?? 0) + 1
}
println(counts)
text
Map{ "the": 3, "ant": 1, "and": 2, "bee": 1, "wasp": 1 }

Build a key that ignores both case and spelling by normalizing first:

ant
func key(s: string): string { s.normalize("NFC").foldCase() }
key("Caf\u{E9}") == key("CAFE\u{301}") // true

Properties and relations ​

String.equalFold compares the folded forms:

ant
for pair in [["Straße", "STRASSE"], ["Ant", "ant"], ["ant", "bee"]] {
  assert(pair[0].equalFold(pair[1]) == (pair[0].foldCase() == pair[1].foldCase()))
}

Folding a folded string changes nothing:

ant
for s in ["Straße", "ΣΟΦΟΣ", "file", ""] {
  assert(s.foldCase().foldCase() == s.foldCase())
}

Possible issues ​

A folded string is a key for comparison, not text to show. It is not always the lower case: String.toLower keeps ß, while foldCase turns it into ss:

ant
"Straße".toLower()  // "straße"
"Straße".foldCase() // "strasse"

Neither foldCase nor == normalizes. An é written as one code point and an e with a combining accent stay different after folding:

ant
let a = "\u{E9}"
let b = "E\u{301}"
a.foldCase() == b.foldCase()                                   // false
a.normalize("NFC").foldCase() == b.normalize("NFC").foldCase() // true

The i flag of a regular expression folds one code point at a time, so it does not match what full folding makes equal. Compare through foldCase instead:

ant
Regex.test(/^straße$/i, "STRASSE")          // false
"straße".foldCase() == "STRASSE".foldCase() // true

Full case folding leaves out the rules for Turkish and Lithuanian, which depend on the language. A dotted capital İ does not fold to a plain i but to an i followed by a combining dot:

ant
"İ".foldCase() == "i\u{307}" // true
"İ".foldCase() == "i"        // false

For the same reason, the dotless ı and the plain i fold apart, even though both have the upper case I. Changing the case of a string before folding it can change the key:

ant
"ı".toUpper()                              // "I"
"ı".foldCase()                             // "ı"
"ı".toUpper().foldCase() == "ı".foldCase() // false

See also ​

String.equalFold · String.toLower · String.toUpper · String.normalize · Array.uniqueBy · Array.groupBy · Regex.test · Strings guide