Check if string contains special characters in Swift

后端 未结 10 2021
时光取名叫无心
时光取名叫无心 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:37

    With Swift 5 you can just do

    if let hasSpecialCharacters =  "your string".range(of: ".*[^A-Za-z0-9].*", options: .regularExpression) != nil {}
    

提交回复
热议问题