This is what I've been doing to change a single letter at a specific location in a word:
let word = "abc" var letters = Array(word.characters) letters[1] = "x" let newWord = String(letters)
Surely there's a more elegant way in Swift 3. But is this really it?
let index = word.index(word.startIndex, offsetBy: 1) let range = index ..< word.index(after: index) word.replacingCharacters(in: range, with: "x")
It works; but it looks hideous.
This code does not generate intermediate Array of Characters.
let word = "abc" let index = word.index(word.startIndex, offsetBy: 1) let newWord = word.replacingCharacters(in: index..<word.index(after: index), with: "x")
or
var mutableWord = "abc" let index = mutableWord.index(mutableWord.startIndex, offsetBy: 1) mutableWord.replaceSubrange(index..<mutableWord.index(after: index), with: "x")
But, in my oponion, this is far from elegant. Maybe we should wait until Swift 4 to get some elegant String APIs...