How can I check to see if a string contains characters of whitespace/alphanumeric/etc?

后端 未结 5 788
执念已碎
执念已碎 2021-02-04 02:19

How can you use the \"ctype.h\" library in Swift to be able to use isAlpha or isSpace on characters? Or is there a better, Swift, way of doing it?

5条回答
  •  时光说笑
    2021-02-04 02:53

    This answer works with text fields. I was going crazy trying to search for whitespace on a UItextfield without searching the string content of it. This works for UItextfields:

    Swift 4:

        if (textField.text?.contains(" "))!{
            print("Has space")
        }else{
            print("Does not have space")
        }
    

    This is for a regular string, also in swift 4

        if string.contains(" "){
            print("Has space")
        }else{
            print("Does not have space")
        }
    

提交回复
热议问题