String.toLower
func String.toLower(s: string): stringString.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}", aniand 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:
String.toLower("Hello, World") // "hello, world"
String.toLower("ANT") // "ant"Call it through .:
"README".toLower() // "readme"Scope
An empty string stays empty, and text with no letters to change, such as digits and spaces, comes back unchanged:
"".toLower() // ""
"room 101".toLower() // "room 101"Every script with case is mapped, not only Latin:
"ÅNGSTRÖM".toLower() // "ångström"
"ПРИВЕТ".toLower() // "привет"The Greek capital sigma becomes ς at the end of a word and σ elsewhere:
"ΣΟΦΟΣ".toLower() // "σοφος"
"ΟΔΟΣ ΣΟΦΟΣ".toLower() // "οδος σοφος"A mapping can make the string longer. The capital İ with a dot lowers to two code points:
"İ".toLower() == "i\u{307}" // true
"İ".toLower().length() // 2Compatibility characters with case are lowered too, such as the Kelvin sign and the title-case digraph Dž:
"\u{212A}".toLower() // "k"
"Dž".toLower() // "dž"In a pipe, the left side is s:
"Content-Type" | String.toLower() // "content-type"Name the argument:
String.toLower(s: "YES") // "yes"Pass it as a function value:
["Ant", "BEE", "Cat"].map(String.toLower) // ["ant", "bee", "cat"]Applications
Make a slug for a URL from a title:
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:
func isImage(name: string): bool {
let lower = name.toLower()
[".png", ".jpg", ".gif"].some({ lower.endsWith(x) })
}
isImage("Photo.JPG") // true
isImage("notes.txt") // falseProperties and relations
Lower case and upper case are not inverses. toUpper turns ß into "SS", and lowering that gives "ss":
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:
"ΣΟΦΟΣ" | 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:
"ISTANBUL".toLower() // "istanbul"
"İ".toLower() == "i" // falseComparing lowered strings misses some matches, because lower case keeps ß. Compare with String.equalFold, or key with String.foldCase:
"Straße".toLower() == "STRASSE".toLower() // false
String.equalFold("Straße", "STRASSE") // trueSee also
String.toUpper · String.foldCase · String.equalFold · String.isLower · String.normalize
