Strings and Runes in Swift

import Foundation

// A Swift string is a collection of Unicode characters.
// Swift uses Unicode scalars as its fundamental unit,
// which is similar to the concept of runes in other languages.

// `s` is a `String` assigned a literal value
// representing the word "hello" in the Thai language.
// Swift string literals are Unicode-encoded text.
let s = "สวัสดี"

// This will produce the number of characters in the string.
print("Character count:", s.count)

// This loop generates the Unicode scalar values of all
// the characters in `s`.
for scalar in s.unicodeScalars {
    print(String(format: "%x ", scalar.value), terminator: "")
}
print()

// To count how many Unicode scalars are in a string, we can use
// the `unicodeScalars` property. Note that some Thai characters
// are represented by Unicode scalars that can span multiple bytes.
print("Unicode scalar count:", s.unicodeScalars.count)

// A `for` loop handles strings specially and iterates
// over each character along with its offset in the string.
for (index, character) in s.enumerated() {
    print("\(character) starts at \(index)")
}

print("\nUsing unicodeScalars")
// We can achieve a similar iteration by using the
// `unicodeScalars` property explicitly.
for (index, scalar) in s.unicodeScalars.enumerated() {
    print("\(scalar) starts at \(index)")
    examineCharacter(scalar)
}

// This demonstrates passing a `Character` value to a function.
func examineCharacter(_ scalar: Unicode.Scalar) {
    // We can compare a `Unicode.Scalar` value to a character literal directly.
    if scalar == "t" {
        print("found tee")
    } else if scalar == "ส" {
        print("found so sua")
    }
}

When you run this Swift code, you’ll see output similar to the following:

Character count: 6
e2a e27 e31 e2a e14 e35 
Unicode scalar count: 6
ส starts at 0
ว starts at 1
ั starts at 2
ส starts at 3
ด starts at 4
ี starts at 5

Using unicodeScalars
ส starts at 0
found so sua
ว starts at 1
ั starts at 2
ส starts at 3
found so sua
ด starts at 4
ี starts at 5

This Swift code demonstrates how to work with strings and Unicode characters. Swift strings are collections of Unicode characters, and Swift provides powerful APIs for working with Unicode text.

The unicodeScalars property is used to access the Unicode scalar values of the characters in the string. This is similar to working with runes in other languages.

Swift’s for loop can iterate over the characters in a string directly, which is convenient for processing text. The enumerated() method is used to get both the index and the character in each iteration.

The examineCharacter function shows how to compare Unicode scalars with character literals. In Swift, you can use single quotes or double quotes for character literals.

Remember that Swift’s string handling is designed to be Unicode-correct, which can sometimes lead to surprises when coming from languages with simpler string models. Always consider the Unicode properties of your strings when processing text in Swift.