更新时间:2023-02-12 19:18:53
使用 replacementOccurrences 以及 String.CompareOptions.regularExpresion 选项。
示例(Swift 3):
Example (Swift 3):
var x = "<Hello, [play^ground+]>"
let y = x.replacingOccurrences(of: "[\\[\\]^+<>]", with: "7", options: .regularExpression, range: nil)
print(y)
输入要在方括号内替换的字符,如此[ \\字符]
Input characters which are to be replaced inside the square brackets like so [\\ Characters]
输出:
7Hello, 7play7ground777