Swift Replace Multiple Characters in String

后端 未结 4 513
日久生厌
日久生厌 2021-02-05 13:07

Below is the following line of code I use to replace an HTML break tag with a carriage return. However, I have other HTML symbols that I need to replace and when I call this lin

4条回答
  •  北海茫月
    2021-02-05 13:59

    Use replacingOccurrences along with a the String.CompareOptions.regularExpresion option.

    Example (Swift 3):

    var x = ""
    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]

    Output:

    7Hello, 7play7ground777
    

提交回复
热议问题