【发布时间】:2017-03-01 23:00:22
【问题描述】:
我正在尝试用另一个子字符串替换一个子字符串,但我收到了这个错误:
cannot convert value of type 'String.Index' (aka 'String.CharacterView.Index') to expected argument type 'Range<String.Index>' (aka 'Range<String.CharacterView.Index>')
这是我的代码:
// This are the position of the substrings:
let rangeOne = strToSort.index(strToSort.startIndex, offsetBy: (i-1))
let rangeTwo = strToSort.index(strToSort.startIndex, offsetBy: (i))
Here is where I try to make the replacement:
strToSort = strToSort.replacingCharacters(in: rangeOne, with: strToSort.substring(with: rangeTwo))
但我收到此错误:
你们中的任何人都知道我做错了什么或解决这个问题吗?
非常感谢您的帮助
【问题讨论】:
-
你想把
EHLLOAGAIN改成HELLOAGAIN? -
var chars = "EHLLOAGAIN".characters.map{String($0)}swap(&chars[0], &chars[1]) -
@LeoDabus,但如何以编程方式进行。我的意思是如果在 x 位置切换子字符串,在 y 位置切换子字符串?
标签: string swift3 swift-playground