Encoding Views: utf8, utf16, unicodeScalars
Work with different encoded representations.
String Encoding Views
A Swift String offers several views onto the same text at different levels: utf8, utf16, and unicodeScalars.
let s = "Hi"
print(Array(s.utf8))
print(Array(s.utf16))The utf8 View
The .utf8 view exposes the string as a sequence of UTF-8 code units (bytes). ASCII characters are one byte each.
let s = "AB"
for byte in s.utf8 {
print(byte)
}All lessons in this course
- Characters and Grapheme Clusters
- Unicode Scalars and Code Points
- Counting and Iterating Characters
- Encoding Views: utf8, utf16, unicodeScalars