简体   繁体   中英

Escaping a single quote in swift

I've used this question

How to remove special characters from string in Swift 2?

Which designates which characters to keep in a string, then removes all other characters. The function works great. The line

Set("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLKMNOPQRSTUVWXYZ1234567890+-*=(),.:!_".characters)

determines what characters to keep. However, when I try to add in the single quote character ('), single quotes are not kept. I tried escaping the single quote with \\' and putting it in regularly, but to no avail. I don't believe single quotes need to be escaped in Swift.

Would someone care to enlighten me on this issue?

Thanks!

Nevermind! As it turns out, I was calling the removeSpecialCharsFromString function inside a do-catch. Once I moved it outside of the do-clause, everything worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM