9

I'm currently using isEqualToString:@"" and it works fine when the textField does have nothing. However, it does not catch the case when the input has only white spaces or tabs. What should I do to make it smarter so that input such as " " will not be allowed.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
OneZero
  • 11,556
  • 15
  • 55
  • 92

2 Answers2

29
NSCharacterSet *charSet = [NSCharacterSet whitespaceCharacterSet];
NSString *trimmedString = [myString stringByTrimmingCharactersInSet:charSet];
if ([trimmedString isEqualToString:@""]) {
    // it's empty or contains only white spaces
}
DrummerB
  • 39,814
  • 12
  • 105
  • 142
9
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length > 0

Use this to test if the string contains characters other than whitespace.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195