A view of a string’s contents as a collection of characters.
In Swift, every string provides a view of its contents as characters. In this view, many individual characters—for example, “é”, “김”, and “”—can be made up of multiple Unicode code points. These code points are combined by Unicode’s boundary algorithms into extended grapheme clusters, represented by the Character type. Each element of a CharacterView collection is a Character instance.
let flowers = "Flowers "
for c in flowers.characters {
print(c)
}
// F
// l
// o
// w
// e
// r
// s
//
//
You can convert a String.CharacterView instance back into a string using the String type’s init(_:)
initializer.
let name = "Marie Curie"
if let firstSpace = name.characters.index(of: " ") {
let firstName = String(name.characters.prefix(upTo: firstSpace))
print(firstName)
}
// Prints "Marie"
https://developer.apple.com/reference/swift/string.characterview