Check if string contains special characters in Swift

后端 未结 10 2052
时光取名叫无心
时光取名叫无心 2020-12-12 18:20

I have to detect whether a string contains any special characters. How can I check it? Does Swift support regular expressions?

var characterSet:NSCharacterSet         


        
10条回答
  •  被撕碎了的回忆
    2020-12-12 18:55

    Depending on the definition of special characters, you could use this:

    let chars =  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    
    chars.canBeConvertedToEncoding(NSASCIIStringEncoding)
    

提交回复
热议问题