Trim whitespace from a string in Swift

let whitespaceSet = NSCharacterSet.whitespaceCharacterSet()
let trimmedString = myTextField.text!.stringByTrimmingCharactersInSet(whitespaceSet)

if trimmedString.characters.count == 0 {
    print("myTextField is blank")
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *