我有一个格式为"this is my title {{and this is what I have to replace}}"
的字符串。
到目前为止,我发现的最好方法是:
let from = originalTitle.firstIndex(of: "{{") ?? 0
let to = originalTitle.firstIndex(of: "}}") ?? 0
if let textToReplace = originalTitle.slicing(from: from, to: to), !textToReplace.isEmpty {
return originalTitle.replacing(textToReplace, with: "XY")
}
return originalTitle
但是,我认为必须有一种更好的方法来明确指示两个占位符或标记,然后替换其中的内容。有什么想法吗?
提前致谢!
最佳答案
到目前为止更好的可能性:
if let from = originalTitle.range(of: "{{")?.lowerBound, let to = originalTitle.range(of: "}}")?.upperBound {
let range = from..<to
return originalTitle.replacingCharacters(in: range, with: "XY")
}
return originalTitle
关于ios - 在Swift中替换指示器之间的子字符串的更好方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44593934/