且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swift替换字符串中的多个字符

更新时间: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