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

前端 未结 11 1721
南旧
南旧 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:44

    Swift 3 example:

    extension String{
        var isContainsLetters : Bool{
            let letters = CharacterSet.letters
            return self.rangeOfCharacter(from: letters) != nil
        }
    }
    

    Usage :

    "123".isContainsLetters // false
    

提交回复
热议问题