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.
Asked
Active
Viewed 1.1k times
9
2 Answers
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