Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何替换字符串中的特定字符?_Swift - Fatal编程技术网

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

Swift 如何替换字符串中的特定字符?,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 = adv

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

您也可以通过以下方式使用该功能:

var s = "bolooooooo"
s.stringByReplacingOccurrencesOfString("o", withString: "e", options: NSStringCompareOptions.LiteralSearch, range: Range<String.Index>(start: advance(s.startIndex, 2), end: s.endIndex))
boleeeeeee