Check if an NSString is just made out of spaces

Suchi picture Suchi · Sep 1, 2011 · Viewed 18.2k times · Source

I want to check if a particular string is just made up of spaces. It could be any number of spaces, including zero. What is the best way to determine that?

Answer

Alexsander Akers picture Alexsander Akers · Sep 1, 2011
NSString *str = @"         ";
NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];
if ([[str stringByTrimmingCharactersInSet: set] length] == 0)
{
    // String contains only whitespace.
}