What is the best way to determine if a string contains a character from a set in Swift

前端 未结 11 1736
南旧
南旧 2020-11-30 01:03

I need to determine if a string contains any of the characters from a custom set that I have defined.

I see from this post that you can use rangeOfString to determine

11条回答
  •  感情败类
    2020-11-30 01:48

    Using Swift 3 to determine if your string contains characters from a specific CharacterSet:

        let letters = CharacterSet.alphanumerics
        let string = "my-string_"
        if (string.trimmingCharacters(in: letters) != "") {
            print("Invalid characters in string.")
        }
        else {
            print("Only letters and numbers.")
        }
    

提交回复
热议问题