IOS: check if a string is an empty string

Is there a way to check if NSString has characters? an example of a string without characters might be:

@ "or @" "or @" \ n "or @" \ n \ n ", I want these lines to be blank and print nslog that tell me that it is emty, what control should I use?

+5
source share
2 answers

You can use this test:

if ([[myString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
    // The string is empty
}
+7
source

You can iterate over each character in a line and check if it is a space character (") or a new line (" \ n "). If not, return false. If you scan the entire line and do not return false, it is" empty ".

Something like that:

NSString* myStr = @"A STRING";
for(int i = 0; i < [myStr length]; i++)
{
    if(!(([myStr characterAtIndex:i] == @' ') || ([myStr characterAtIndex:i] == @'\n')))
    {
        return false;
    }
}
0
source

All Articles