swift - 如何替换字符串中的特定字符?

标签 swift

我试图替换字符串中的特定字符。我想将第二个 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/

相关文章:

ios - Dictionaries of Dictionaries词典语法理解词典

iOS Swift3 TextView 行在错误的位置

Swift 中带有普通和粗体文本标签的 iOS 选择器 View

ios - Webview 不适用于 iOS9

swift - 未保存核心数据关系数据

ios - Swift:将 xib 添加到 UIView

ios - 如何快速取出字符串的一部分

swift - 如何从 IOS 客户端连接到 Spring WebSocket 服务器?

ios - 如何正确地从 xcode 框架调用 loadView()

ios - 在增强现实应用程序的 Swift 项目中使用 Vuforia iOS SDK