replaceOccurrences(of:“'”,with:“”)在TextField.text上不起作用

时间:2019-07-26 13:29:41

标签: ios swift

我从uitextfield框中得到输入。我可以将quotes(“'”)替换为empty(“”“)。使用

textfield.text.replacingOccurrences(of: "'", with: "") 

不起作用

textfield.text = "name's"
let trim = textfield.text?.replacingOccurrences(of: "'", with: "")

预期输出:

  

名称

实际输出:

  

姓名

1 个答案:

答案 0 :(得分:0)

text.replacingOccurrences可以与正则表达式一起使用,因此使用组['`´]可以在这里工作(我不知道有任何元字符)。正如@Rob在评论中提到的那样,可能值得进一步扩展模式,例如[‘’’ʻ´]或[’’‛'’’`❛❜]或使用“ \ p {Quotation_Mark}”

let trim = text.replacingOccurrences(of: "['`´]", with: "", options: .regularExpression)

它不能代替è或é

let text = "name's are Josè and André, strings are `abc` and ´def´"
let trim = text.replacingOccurrences(of: "['`´]", with: "", options: .regularExpression)

print(trim)

收益

  

名字是Josè和André,字符串是abc和def