0Pricing
Swift Academy · Lesson

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

  1. Characters and Grapheme Clusters
  2. Unicode Scalars and Code Points
  3. Counting and Iterating Characters
  4. Encoding Views: utf8, utf16, unicodeScalars
← Back to Swift Academy