Skip to main content

DSA: String and Character Array Conversion

DSA

Convert a Swift String to a character array for indexed DSA work, then rebuild the String.

Swift strings are not integer-indexed. Convert one to [Character] when an interview problem needs array-style indexing or in-place mutation.

let text = "swift"

let characters: [Character] = Array(text)
// ["s", "w", "i", "f", "t"]

let rebuilt = String(characters)
// "swift"

Each element is a Swift Character, so a user-perceived character such as an emoji stays together rather than being split into bytes.