String.isLetter
func String.isLetter(s: string): boolString.isLetter reports whether every code point of s is a letter (L). It is false on "".
Details
truewhen every code point ofshas a general category in L;falseon"".- Letters of any script count. The test is per code point: a combining accent is a mark, not a letter.
Basic examples
String.isLetter("ant") // true
String.isLetter("A") // true
String.isLetter("ant1") // falseCall it through .:
"Colony".isLetter() // trueScope
The empty string has no code points to test, and gives false:
"".isLetter() // falseEvery code point must be a letter, so a space, a digit, a hyphen, or an underscore anywhere makes it false:
"red ant".isLetter() // false
"ant2".isLetter() // false
"re-do".isLetter() // false
"my_var".isLetter() // falseLetters of other scripts count:
"Привет".isLetter() // true
"αβγ".isLetter() // true
"مرحبا".isLetter() // true
"日本語".isLetter() // true
"ß".isLetter() // trueEach kind of letter counts: upper case Lu, lower case Ll, title case Lt, modifier Lm, and other Lo:
"DŽ".isLetter() // true
"dž".isLetter() // true
"Dž".isLetter() // true
"ʰ".isLetter() // true
"ア".isLetter() // trueA Roman numeral looks like a letter but is a number, Nl. An emoji is a symbol:
"Ⅻ".isLetter() // false
"🐜".isLetter() // falseTest one code point at a time with String.chars, here in a pipe:
"a1 b" | String.chars() | Array.map({ x.isLetter() }) // [true, false, false, true]Pass it to a higher-order function as a function value:
["ant", "42", "", "bee"].filter(String.isLetter) // ["ant", "bee"]Applications
Keep only the letters of a text:
let code = "A1-B2_c3"
String.concat(code.chars().filter({ x.isLetter() })) // "ABc"Count the letters of a sentence:
"Hi, ants! 3 of you?".chars().countWhere({ x.isLetter() }) // 11Split a text into words, where a word is a run of letters:
func words(text: string): Array[string] {
let out: Array[string] = []
var word = ""
for c in text {
if c.isLetter() {
word = word + c
} else if word != "" {
out.push(word)
word = ""
}
}
if word != "" {
out.push(word)
}
out
}
words("Ants, bees & wasps: 3 orders?") // ["Ants", "bees", "wasps", "orders"]Check that a name starts with a letter before you accept it:
type Check = { name: string, ok: bool }
func check(name: string): Check {
let chars = name.chars()
Check{ name: name, ok: !chars.isEmpty() && chars[0].isLetter() }
}
for c in ["ant", "_tmp", "9lives", ""].map(check) {
println("${c.name}: ${c.ok}")
}ant: true
_tmp: false
9lives: false
: falseProperties and relations
String.isLetter is true exactly when the string is not empty and every code point has a category that starts with L:
for s in ["", "a", "Ant", "日本", "Dž", "ʰ", "a1", "Ⅻ", "e\u{301}", "_"] {
let byCategory = s != "" && s.chars().every({ x.category().startsWith("L") })
assert(s.isLetter() == byCategory)
}An upper-case or lower-case letter is a letter, so String.isUpper and String.isLower imply String.isLetter, but not the other way around:
for s in ["A", "a", "Dž", "日", "ʰ", "1"] {
assert(!s.isUpper() || s.isLetter())
assert(!s.isLower() || s.isLetter())
}
"日".isLetter() // true
"日".isUpper() // false
"日".isLower() // falseFor nonempty a and b, a + b is all letters exactly when both a and b are:
for a in ["ab", "日", "1"] {
for b in ["c", "ß", " "] {
assert(String.isLetter(a + b) == (a.isLetter() && b.isLetter()))
}
}Possible issues
An accented letter written as a base letter and a combining accent is two code points, and the accent is a mark, not a letter. Normalize to NFC first to compose the pair when Unicode has one code point for it:
let e = "e\u{301}"
e.isLetter() // false
"\u{301}".isMark() // true
e.normalize("NFC").isLetter() // trueReal names hold more than letters: an apostrophe, a hyphen, a space. String.isLetter rejects them:
"O'Brien".isLetter() // false
"Jean-Luc".isLetter() // false
"Mary Ann".isLetter() // falseString.isLetter tests that the whole string is letters, not that it holds one. Use Array.some for that:
"a1".isLetter() // false
"a1".chars().some({ x.isLetter() }) // trueA letter is not only a to z. To accept ASCII letters only, test the code point:
func isAsciiLetter(c: string): bool {
let n = c.codePoint()
(n >= 65 && n <= 90) || (n >= 97 && n <= 122)
}
"é".isLetter() // true
isAsciiLetter("é") // false
isAsciiLetter("Z") // trueSee also
String.isUpper · String.isLower · String.isDigit · String.isNumber · String.isMark · String.category · String.inScript · String.normalize · String.chars · Array.every · Array.some
