Skip to content

String.toLower ​

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

String.toLower returns s in lower case by Unicode's full case mappings. The string is mapped whole, so a final Greek sigma becomes ς, and the mappings that depend on a language, such as Turkish, are not applied.

Details ​

  • The result can be longer than s: "İ" becomes "i\u{307}", an i and a combining dot.
  • The string is mapped whole: a capital sigma at the end of a word becomes ς, elsewhere σ.
  • The rules for Turkish and Lithuanian, which depend on the language, are not applied.

Basic examples ​

Lower-case a string:

ant
String.toLower("Hello, World") // "hello, world"
String.toLower("ANT")          // "ant"

Call it through .:

ant
"README".toLower() // "readme"

Scope ​

An empty string stays empty, and text with no letters to change, such as digits and spaces, comes back unchanged:

ant
"".toLower()         // ""
"room 101".toLower() // "room 101"

Every script with case is mapped, not only Latin:

ant
"ÅNGSTRÖM".toLower() // "ångström"
"ПРИВЕТ".toLower()   // "привет"

The Greek capital sigma becomes ς at the end of a word and σ elsewhere:

ant
"ΣΟΦΟΣ".toLower()      // "σοφος"
"ΟΔΟΣ ΣΟΦΟΣ".toLower() // "οδος σοφος"

A mapping can make the string longer. The capital İ with a dot lowers to two code points:

ant
"İ".toLower() == "i\u{307}" // true
"İ".toLower().length()      // 2

Compatibility characters with case are lowered too, such as the Kelvin sign and the title-case digraph Dž:

ant
"\u{212A}".toLower() // "k"
"Dž".toLower()        // "dž"

In a pipe, the left side is s:

ant
"Content-Type" | String.toLower() // "content-type"

Name the argument:

ant
String.toLower(s: "YES") // "yes"

Pass it as a function value:

ant
["Ant", "BEE", "Cat"].map(String.toLower) // ["ant", "bee", "cat"]

Applications ​

Make a slug for a URL from a title:

ant
func slug(title: string): string {
  title
    | String.toLower()
    | String.fields()
    | String.join("-")
}

slug("Hello  Ant World") // "hello-ant-world"

Check a file name for an image extension however it is spelled:

ant
func isImage(name: string): bool {
  let lower = name.toLower()
  [".png", ".jpg", ".gif"].some({ lower.endsWith(x) })
}

isImage("Photo.JPG") // true
isImage("notes.txt") // false

Properties and relations ​

Lower case and upper case are not inverses. toUpper turns ß into "SS", and lowering that gives "ss":

ant
assert("ß".toUpper().toLower() == "ss")
assert("ß".toLower() != "ß".toUpper().toLower())

Possible issues ​

Mapping each character on its own loses the final sigma, since the sigma depends on what follows it:

ant
"ΣΟΦΟΣ" | String.chars() | Array.map(String.toLower) | String.join("") // "σοφοσ"
"ΣΟΦΟΣ".toLower()                                                      // "σοφος"

The Turkish rules are not applied, so the dotless I lowers to i, not ı, and İ gains a combining dot:

ant
"ISTANBUL".toLower() // "istanbul"
"İ".toLower() == "i" // false

Comparing lowered strings misses some matches, because lower case keeps ß. Compare with String.equalFold, or key with String.foldCase:

ant
"Straße".toLower() == "STRASSE".toLower() // false
String.equalFold("Straße", "STRASSE")     // true

See also ​

String.toUpper · String.foldCase · String.equalFold · String.isLower · String.normalize