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

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

    Use like this in swift 4.2

        var string = "Hello, World!"
        let charSet = NSCharacterSet.letters
        if string.rangeOfCharacter(from:charSet)?.isEmpty == false{  
          print ("String contain letters");
        }
    

提交回复
热议问题