To replace a character at a specific index in a string in Swift, you can use string manipulation functions like replacingCharacters(in:with:)
.
In this example,
str
.replacingCharacters(in:with:)
method to replace the character at index index
with the character newChar
.str
.var str = "Hello World"
let index = str.index(str.startIndex, offsetBy: 6)
let newChar: Character = "x"
str = String(str.prefix(upTo: index)) + String(newChar) + String(str.suffix(from: str.index(after: index)))
print(str)
Hello xorld
In this tutorial, we learned How to Replace Character at Specific Index in String in Swift language with well detailed examples.