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,383
  • 9
  • 144
  • 191
OneZero
  • 10,918
  • 11
  • 54
  • 88

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,275
  • 12
  • 103
  • 141
9
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length > 0

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

Léo Natan
  • 56,383
  • 9
  • 144
  • 191