String.isUpper
func String.isUpper(s: string): boolString.isUpper reports whether every code point of s is an upper-case letter (Lu). It is false on "".
Details
truewhen every code point ofshas the general category Lu;falseon"".- Only Lu counts: a title-case letter such as
Dž, a Roman numeral such asⅫ, and a circled letter such asⒶare not upper-case letters.
Basic examples
String.isUpper("ANT") // true
String.isUpper("Ant") // false
String.isUpper("Q") // trueCall it through .:
"NASA".isUpper() // trueScope
The empty string has no code points to test, and gives false:
String.isUpper("") // falseEvery code point must be an upper-case letter, so a digit, an underscore, or a space makes the result false:
String.isUpper("HTTP2") // false
String.isUpper("MAX_SIZE") // false
String.isUpper("NEW YORK") // falseUpper-case letters of every script count, including the capital sharp s ẞ and the dotted İ:
String.isUpper("ÉTÉ") // true
String.isUpper("ΣΩ") // true
String.isUpper("ЖУК") // true
String.isUpper("ẞ") // true
String.isUpper("İ") // trueThe full-width A and the mathematical bold 𝐀 are Lu too:
String.isUpper("A") // true
String.isUpper("𝐀") // trueSome code points look like capitals but are not in Lu: the Roman numeral Ⅻ is a number, the circled Ⓐ is a symbol, and the title-case Dž is Lt:
String.isUpper("Ⅻ") // false
String.category("Ⅻ") // "Nl"
String.isUpper("Ⓐ") // false
String.category("Ⓐ") // "So"
String.isUpper("Dž") // false
String.category("Dž") // "Lt"Test one code point at a time with String.chars, here in a pipe:
"aBcD" | String.chars() | Array.map({ x.isUpper() }) // [false, true, false, true]Pass it to a higher-order function as a function value:
["OK", "Ok", "", "A1", "ΔΣ"].filter(String.isUpper) // ["OK", "ΔΣ"]Applications
Build an acronym from the capitals of a phrase:
let phrase = "Portable Network Graphics"
String.concat(phrase.chars().filter({ x.isUpper() })) // "PNG"Split a PascalCase name into words at each upper-case letter:
func words(name: string): Array[string] {
let out: Array[string] = []
var word = ""
for c in name {
if word != "" && c.isUpper() {
out.push(word)
word = ""
}
word += c
}
if word != "" {
out.push(word)
}
out
}
words("HttpRequestParser") // ["Http", "Request", "Parser"]
words("HTTPServer") // ["H", "T", "T", "P", "Server"]Each upper-case letter starts a new word, so a run of capitals such as an acronym splits letter by letter.
Flag a message whose words are all in capitals:
func shouting(text: string): bool {
let ws = text.fields()
!ws.isEmpty() && ws.every(String.isUpper)
}
for msg in ["STOP RIGHT THERE", "Stop right there", "OK THEN", "NO 2"] {
println("${msg}: ${shouting(msg)}")
}STOP RIGHT THERE: true
Stop right there: false
OK THEN: true
NO 2: falseProperties and relations
String.isUpper is true exactly when the string is not empty and every code point has the category "Lu":
for s in ["", "A", "ABC", "Ab", "Dž", "Ⅻ", "Ω", "A1", "日"] {
let byCategory = s != "" && s.chars().every({ x.category() == "Lu" })
assert(String.isUpper(s) == byCategory)
}An upper-case letter is a letter, so String.isUpper implies String.isLetter, and no string is both upper and lower case:
for s in ["A", "a", "Dž", "日", "Ab", "Σ", "ẞ"] {
assert(!String.isUpper(s) || String.isLetter(s))
assert(!(String.isUpper(s) && String.isLower(s)))
}Possible issues
String.isUpper does not ask whether String.toUpper would leave the string unchanged. A string with a digit, a space, or an underscore is already in upper case, but is not all upper-case letters:
String.toUpper("MAX_SIZE") == "MAX_SIZE" // true
String.isUpper("MAX_SIZE") // falseTo check that upper-casing leaves a string unchanged, the test a constant name such as "MAX_SIZE" passes, compare it with String.toUpper(s) as above.
The same accented capital can be one code point or a letter followed by a combining accent. The combining accent is a mark, Mn, so the second spelling is not all upper-case letters. Normalize to NFC first:
let decomposed = "ÉTÉ".normalize("NFD")
String.isUpper(decomposed) // false
String.isUpper(decomposed.normalize("NFC")) // trueA title-case letter such as Dž is not upper case, but String.toUpper turns it into the upper-case DŽ. Upper-casing can also change the length: String.toUpper turns the lower-case ß into two letters:
String.isUpper("Dž") // false
String.toUpper("Dž") // "DŽ"
String.isUpper(String.toUpper("Dž")) // true
String.toUpper("straße") // "STRASSE"String.isUpper tests that the whole string is upper-case letters, not that it holds one. Use Array.some for that:
String.isUpper("abC") // false
"abC".chars().some({ x.isUpper() }) // trueSee also
String.isLower · String.isLetter · String.toUpper · String.toLower · String.category · String.normalize · String.chars · Array.every · Array.some
