String.graphemes โ
func String.graphemes(s: string): Array[string]String.graphemes returns the extended grapheme clusters of s (Unicode UAX #29), the units a reader sees as characters.
Details โ
- Splits
sinto the units a reader sees as one character (UAX #29 extended grapheme clusters). Joining the elements givessback. - One grapheme can be several code points: a letter with combining accents, a flag, an emoji with a skin tone.
String.charssplits these apart. - An empty
sgives[]. - It does not normalize:
"\u{E9}"and"e\u{301}"are each one grapheme, and still different strings.
Basic examples โ
Split a word into its characters:
String.graphemes("ant") // ["a", "n", "t"]A flag is one grapheme, though it is two code points:
String.graphemes("๐จ๐ญ!") // ["๐จ๐ญ", "!"]
"๐จ๐ญ!".chars() // ["๐จ", "๐ญ", "!"]Count what a reader sees with len:
len(String.graphemes("๐๐ฝ")) // 1
"๐๐ฝ".length() // 2Scope โ
An empty string gives an empty array:
"".graphemes() // []A combining accent stays with the letter before it:
len("e\u{301}a".graphemes()) // 2
len("noe\u{308}l".graphemes()) // 4An accent at the very start has no letter to attach to, so it is a grapheme of its own:
len("\u{301}a".graphemes()) // 2A family emoji is three people and two zero width joiners, five code points in one grapheme:
let family = "๐จโ๐ฉโ๐ง"
family.length() // 5
len(family.graphemes()) // 1Two flags in a row pair up their regional indicators, and a lone indicator left over is a grapheme of its own:
"๐จ๐ญ๐ฉ๐ช".graphemes() // ["๐จ๐ญ", "๐ฉ๐ช"]
"๐จ๐ญ๐ฉ".graphemes() // ["๐จ๐ญ", "๐ฉ"]A Windows line break, "\r\n", is one grapheme:
"a\r\nb".graphemes() // ["a", "\r\n", "b"]Three Hangul jamo combine into one syllable, and a Devanagari conjunct holds together:
len("\u{1100}\u{1161}\u{11A8}".graphemes()) // 1
len("เคจเคฎเคธเฅเคคเฅ".graphemes()) // 3Call it through . on a string, or put the string first in a pipe:
"x\u{303}y".graphemes().map({ x.length() }) // [2, 1]
"Zoรซ" | String.graphemes() // ["Z", "o", "รซ"]The parameter is named s:
String.graphemes(s: "ok") // ["o", "k"]As a function value, it splits each element of an array:
["hi", "๐๐"].map(String.graphemes) // [["h", "i"], ["๐", "๐"]]Applications โ
Reverse text without moving an accent onto another letter or splitting a flag. Reversing the code points instead breaks both:
func reverse(s: string): string {
s.graphemes().reverse() | String.concat()
}
println(reverse("noe\u{308}l ๐จ๐ญ"))
println("noe\u{308}l ๐จ๐ญ".chars().reverse() | String.concat())๐จ๐ญ leฬon
๐ญ๐จ lฬeonShorten text to a number of characters for display, never cutting one in half:
func truncate(s: string, n: int): string {
let gs = s.graphemes()
if len(gs) <= n {
return s
}
String.concat(gs.slice(0, n)) + "โฆ"
}
println(truncate("Ant ๐ language", 5))
println(truncate("๐จโ๐ฉโ๐ง๐จโ๐ฉโ๐ง๐จโ๐ฉโ๐ง", 2))Ant ๐โฆ
๐จโ๐ฉโ๐ง๐จโ๐ฉโ๐งโฆTake the initials of a name, keeping each one whole:
func initials(name: string): string {
name.fields().map({ x.graphemes()[0] }) | String.concat()
}
initials("E\u{301}mile Zola") // "EฬZ"Properties and relations โ
Joining the graphemes gives the string back, and every grapheme is a non-empty string:
for s in ["", "ant", "noe\u{308}l", "๐จ๐ญ๐ฉ", "๐จโ๐ฉโ๐ง\r\n"] {
let gs = s.graphemes()
assert(String.concat(gs) == s)
assert(gs.every({ !x.isEmpty() }))
}A grapheme holds one code point or more, so there are never more graphemes than code points:
for s in ["", "ant", "e\u{301}", "๐๐ฝ", "เคจเคฎเคธเฅเคคเฅ"] {
assert(len(s.graphemes()) <= s.length())
}For text with no combining marks, joiners, or flags, the graphemes are the code points:
let s = "plain ASCII text"
assert(s.graphemes() == s.chars())Possible issues โ
String.length counts code points, not graphemes. Count graphemes with len of the result:
"e\u{301}".length() // 2
len("e\u{301}".graphemes()) // 1A for loop over a string steps through its code points, not its graphemes. Loop over the graphemes to visit the characters a reader sees:
let s = "e\u{301}"
var points = 0
for c in s {
points += 1
}
var chars = 0
for g in s.graphemes() {
chars += 1
}
assert(points == 2)
assert(chars == 1)String.slice takes code point indices, so it can cut a grapheme in half. Here it keeps the first person and a joiner of the family:
"๐จโ๐ฉโ๐ง".slice(0, 2).length() // 2
"๐จโ๐ฉโ๐ง".graphemes()[0].length() // 5String.graphemes does not normalize. The two spellings of "รฉ" are each one grapheme, but not the same string. Normalize first to compare them:
"\u{E9}".graphemes() == "e\u{301}".graphemes() // false
"e\u{301}".normalize("NFC").graphemes() // ["รฉ"]See also โ
String.chars ยท String.length ยท String.slice ยท String.normalize ยท String.concat ยท String.codePoint ยท String.toCodePoints ยท Array.reverse
