Counting and Iterating Characters
Iterate text correctly across emoji and accents.
Counting Characters
The count property of a String returns the number of Character values, which equals the number of user-perceived characters.
let word = "Swift"
print(word.count)Count Reflects Visible Characters
Because Swift counts grapheme clusters, an accented letter or emoji counts as one, matching what a reader sees.
let cafe = "caf\u{e9}"
print(cafe.count)All lessons in this course
- Characters and Grapheme Clusters
- Unicode Scalars and Code Points
- Counting and Iterating Characters
- Encoding Views: utf8, utf16, unicodeScalars