String.isLower
func String.isLower(s: string): boolString.isLower reports whether every code point of s is a lower-case letter (Ll). It is false on "".
Details
truewhen every code point ofshas the general category Ll;falseon"".- Only Ll counts: a modifier letter such as
ʰ, a title-case letter such asDž, and a combining accent are not lower-case letters.
Basic examples
String.isLower("ant") // true
String.isLower("Ant") // false
String.isLower("x") // trueCall it through .:
"colony".isLower() // trueScope
The empty string has no code points to test, and gives false:
"".isLower() // falseEvery code point must be a lower-case letter, so a space, a digit, a hyphen, or an underscore makes the result false:
"hello world".isLower() // false
"ab1".isLower() // false
"well-known".isLower() // false
"snake_case".isLower() // falseLower-case letters of every script count, with their accents when the accent is part of the code point:
"été".isLower() // true
"straße".isLower() // true
"π".isLower() // true
"жук".isLower() // true
"fi".isLower() // trueHan characters such as 日 are letters of category Lo, not lower-case letters:
"日本".isLower() // false
"日".category() // "Lo"The title-case letter Dž is neither lower nor upper case, and the modifier letter ʰ is not a lower-case letter either:
"Dž".isLower() // false
"Dž".isUpper() // false
"Dž".category() // "Lt"
"ʰ".isLower() // false
"ʰ".category() // "Lm"Test one code point at a time with String.chars, here in a pipe:
"aBc" | String.chars() | Array.map({ x.isLower() }) // [true, false, true]Pass it to a higher-order function as a function value:
["id", "Name", "", "x2", "età"].filter(String.isLower) // ["id", "età"]Applications
Split a camelCase name into words before each code point that is not a lower-case letter:
func words(name: string): Array[string] {
let out: Array[string] = []
var word = ""
for c in name {
if word != "" && !c.isLower() {
out.push(word)
word = ""
}
word += c.toLower()
}
if word != "" {
out.push(word)
}
out
}
words("parseHttpRequest") // ["parse", "http", "request"]Count the lower-case letters of a password:
"Tr0ub4dor&3".chars().countWhere({ x.isLower() }) // 6Accept a package name only when it is all lower-case letters:
for name in ["json", "Json", "json5", "время"] {
if name.isLower() {
println("${name}: ok")
} else {
println("${name}: use lower-case letters only")
}
}json: ok
Json: use lower-case letters only
json5: use lower-case letters only
время: okProperties and relations
String.isLower is true exactly when the string is not empty and every code point has the category "Ll":
for s in ["", "a", "abc", "aB", "Dž", "ʰ", "π", "a1", "日"] {
let byCategory = s != "" && s.chars().every({ x.category() == "Ll" })
assert(s.isLower() == byCategory)
}A lower-case letter is a letter, so String.isLower implies String.isLetter, and no string is both lower and upper case:
for s in ["a", "A", "Dž", "日", "aB", "π", "Ω"] {
assert(!s.isLower() || s.isLetter())
assert(!(s.isLower() && s.isUpper()))
}Possible issues
String.isLower does not ask whether String.toLower would leave the string unchanged. A string with a digit or a space is already in lower case, but is not all lower-case letters:
"ab 1".toLower() == "ab 1" // true
"ab 1".isLower() // falseTo check that String.toLower leaves a string unchanged, compare s == s.toLower(). To check that no code point is an upper-case letter, use !s.chars().some({ x.isUpper() }):
!"ab 1".chars().some({ x.isUpper() }) // trueThe same accented letter 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 lower-case letters. Normalize to NFC first:
let decomposed = "été".normalize("NFD")
decomposed.isLower() // false
decomposed.normalize("NFC").isLower() // trueA lower-case letter can have an upper-case form longer than itself. ß is lower case, and String.toUpper turns it into two letters:
"ß".isLower() // true
"ß".toUpper() // "SS"String.isLower tests that the whole string is lower-case letters, not that it holds one. Use Array.some for that:
"ABc".isLower() // false
"ABc".chars().some({ x.isLower() }) // trueSee also
String.isUpper · String.isLetter · String.toLower · String.toUpper · String.category · String.normalize · String.chars · Array.every · Array.some
