String.toUpper
func String.toUpper(s: string): stringString.toUpper returns s in upper case by Unicode's full case mappings, so the length can change. The mappings that depend on a language, such as Turkish, are not applied.
Details
- One code point can become several:
"ß"becomes"SS". Code points without an upper-case form, such as digits, stay as they are. - The rules for Turkish and Lithuanian, which depend on the language, are not applied, so
"i"becomes"I", not"İ".
Basic examples
String.toUpper("hello") // "HELLO"
String.toUpper("straße") // "STRASSE"Call it through . on any string:
let lang = "ant"
lang.toUpper() // "ANT"Scope
The empty string stays empty, and text with no letters comes back unchanged:
"".toUpper() // ""
"123 !?".toUpper() // "123 !?"Letters with accents and letters of other alphabets have upper-case forms too:
"élan".toUpper() // "ÉLAN"
"σοφος".toUpper() // "ΣΟΦΟΣ"
"привет".toUpper() // "ПРИВЕТ"Scripts without case, such as Japanese, pass through:
"日本語".toUpper() // "日本語"A ligature expands into its letters, so the result can be longer than s:
"fi".toUpper() // "FI"
"ffi".toUpper() // "FFI"
"fi".toUpper().length() // 2
"straße".toUpper().length() // 7A combining accent is a code point of its own and stays after the letter, which is mapped:
"e\u{301}".toUpper() == "E\u{301}" // trueIn a pipeline, the string is the first argument:
"warning" | String.toUpper() // "WARNING"String.toUpper is a function value too, so it can go straight to Array.map:
["get", "post"].map(String.toUpper) // ["GET", "POST"]Applications
Name a constant from a phrase:
func constName(phrase: string): string {
phrase
| String.fields()
| Array.map(String.toUpper)
| String.join("_")
}
constName("max retry count") // "MAX_RETRY_COUNT"Take the initials of a name:
func initials(name: string): string {
name
| String.fields()
| Array.map({ x.slice(0, 1).toUpper() })
| String.concat()
}
initials("ada lovelace") // "AL"Capitalize the first character a reader sees, with String.graphemes, so an accent written as its own code point stays with its letter:
func capitalize(s: string): string {
let gs = s.graphemes()
if gs.isEmpty() {
return s
}
gs[0].toUpper() + String.concat(gs.slice(1, gs.length()))
}
println(capitalize("e\u{301}lan") == "E\u{301}lan")
println(capitalize("ant"))
println(capitalize("") == "")true
Ant
trueProperties and relations
On these strings, upper-casing twice gives the same result as upper-casing once:
for s in ["straße", "fine", "σοφος", "Ant 2", ""] {
assert(s.toUpper().toUpper() == s.toUpper())
}Upper-casing "hello" gives a string that String.isUpper accepts. A digit or a space is not an upper-case letter, so it makes isUpper false:
assert("hello".toUpper().isUpper())
assert(!"hello 1".toUpper().isUpper())Upper-casing and then lower-casing with String.toLower does not give back the original when the mapping expands:
"straße".toUpper().toLower() // "strasse"
"Ant".toUpper().toLower() // "ant"Possible issues
The length can change, so a code point index found in s may not point at the same text in String.toUpper(s):
let s = "straße!"
s.indexOf("!") // 6
s.toUpper().indexOf("!") // 7String.toUpper gives the upper-case letter, not the title-case one. The digraph "dž" becomes "DŽ", not "Dž":
"džemal".toUpper() // "DŽEMAL"Turkish rules are not applied: the dotted "i" becomes the dotless "I":
"istanbul".toUpper() // "ISTANBUL"To compare without regard to case, use String.equalFold or String.foldCase, which are made for it:
String.equalFold("Straße", "STRASSE") // trueThe function on the right of a pipe needs its parentheses:
println("ant" | String.toUpper) // error: the right side of a pipe must be a callA nullable string, such as the result of Map indexing, is a compile error. Narrow it first, or give a default with ??:
let user = Map{ name: "ann" }
println(String.toUpper(user["name"])) // error: cannot use string | null where string is expectedlet user = Map{ name: "ann" }
(user["name"] ?? "").toUpper() // "ANN"See also
String.toLower · String.foldCase · String.equalFold · String.isUpper · String.graphemes · String.normalize
