24

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?

This question is answered, but it doesn't seem to work: Swift: how to find out if letter is Alphanumeric or Digit

It doesn't specify how to import the library. Could someone point me in the right direction?

Here's what I've got so far:

extension String {
    subscript (i : Int) -> String {
        return String(Array(self)[i])
    }
}

let whitespace = NSCharacterSet.whitespaceCharacterSet()

let phrase = "Test case"

for var i=0; i<countElements(phrase); i++ {
    if whitespace.characterIsMember(phrase[i]) { //error
        println("char is whitespace")
    }
}
Community
  • 1
  • 1
hazrpg
  • 481
  • 1
  • 3
  • 19

6 Answers6

35

Use NSCharacter on the entire string,not character-by-character:

let whitespace = NSCharacterSet.whitespaceCharacterSet()

let phrase = "Test case"
let range = phrase.rangeOfCharacterFromSet(whitespace)

// range will be nil if no whitespace is found
if let test = range {
    println("whitespace found")
}
else {
    println("whitespace not found")
}

Output:

whitespace found
zaph
  • 111,848
  • 21
  • 189
  • 228
11

I created a String extension that does exactly this, hope it's useful.

extension String {
    func containsWhitespaceAndNewlines() -> Bool {
        return rangeOfCharacter(from: .whitespacesAndNewlines) != nil
    }
}

// Usage
"hello, world!".containsWhitespaceAndNewlines() // true
"hello,world!".containsWhitespaceAndNewlines() // false
Islam
  • 3,654
  • 3
  • 30
  • 40
Samuel B.
  • 363
  • 1
  • 4
  • 10
10

Shorter extension (swift 4.1)

extension String {
    var containsWhitespace : Bool {
        return(self.rangeOfCharacter(from: .whitespacesAndNewlines) != nil)
    }
}

You can change the .whitespacesAndNewlines with any other CharacterSet like this:

extension String {
    var containsDigits : Bool {
        return(self.rangeOfCharacter(from: CharacterSet.decimalDigits) != nil)
    }
}
Rick Pasveer
  • 480
  • 7
  • 12
2

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")
    }
Joel
  • 327
  • 4
  • 3
1

For Swift 5

extension String {

    func containsWhiteSpace() -> Bool {

        // check if there's a range for a whitespace
        let range = self.rangeOfCharacter(from: .whitespacesAndNewlines)

        // returns false when there's no range for whitespace
        if let _ = range {
            return true
        } else {
            return false
        }
    }
}
kauai
  • 489
  • 1
  • 6
  • 9
-1

This function will tell you if there is a whitespace between a string. for example if you are trying to make sure the user enter a first and last name

Swift 5

func validatTextField() -> Bool {
        var isValid = false
        if textField.text!.contains(" ") && !textField.text!.isEmpty{
            let cardHolderChar = textField.text!
            for i in cardHolderChar {
                if i == " " && i == cardHolderChar.last {
                    isValid = false
                }
                else {
                    isValid = true
                }
            }
        }
        return isValid
    }
Adrien
  • 1,579
  • 6
  • 25