我试图替换字符串中的特定字符。我想将第二个 o
替换为 e
。我尝试使用:
var s = "bolo"
var charIndex = advance(1, 1)
s.replaceRange(Range(start: charIndex, end: charIndex), with: "e")
println(s)
最佳答案
使用advance时只需要指定字符串startIndex(s.startIndex)即可:
var s = "bolo"
let charIndex = advance(s.startIndex, 3)
s.replaceRange(charIndex...charIndex, with: "e")
println(s) // "bole"
关于swift - 如何替换字符串中的特定字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29779959/