9

What is the best was to determine if an NSString is empty? Right now I am using the following:

if (string == nil || [string isEqualToString:@""]) { // do something }

Thanks for any advice.

3 Answers 3

24
if ([string length] == 0) {
    // do something
}

If the string is nil, then the message to nil will return zero, and all will still be well.

Sign up to request clarification or add additional context in comments.

7 Comments

+1 for your answer. what about whitespaces? What if you don't want to count them? Any handy way?
@cocoafan Not concisely. If you need to do that frequently, I'd suggest adding a category to NSString, something like isNotEmpty, that you can do your custom checks in. Then you can just do if ([string isNotEmpty]) { … } and it will also handle nils properly…
You may still want to know why the length returned 0 so it might be useful sometimes to check for nil, after getting the zero.
You would want to check against a temp string created by splitting the string with componentsSeparatedByCharactersInSet: [NSCharacterset whiteSpaceCharacterSet] then joining the resulting array of string components.
@Sulthan I completely agree with you, but being negative is important to this particular method, since messages to nil return NO. Maybe hasValue or something of that sort would be better.
|
2

This will not only check if there is nothing in the string but will also return false if it is just whitespace.

NSString *tempString = [myString stringByReplacingOccurrencesOfString:@" " withString:@""];


if ([tempString length] != 0) {
    //There is something in the string.
} else {
    //There is nothing or it is just whitespace.
}

3 Comments

This only removes leading and trailing whitespace
You know what, you're right. I was doing a lot of experimentation and must have copied and pasted the wrong code. It's corrected. This is much more efficient.
Whatever else it might be, -stringByReplacingOccurrencesOfString:withString: is not especially efficient. Much better to test what you're actually looking for: non-whitespace characters. !myString || [myString rangeOfCharacterFromSet:[[NSCharacterSet whitespaceCharacterSet] invertedSet].location == NSNotFound (In this case, you have to test whether myString is nil explicitly because an NSRange resulting from messaging nil won't have NSNotFound in its location field.)
0

Not good solving

[nil length] is 0

(0==0) is 1

then ([string length] == 0) will be 1. Although it is wrong.

The best way is

if (![string length]) {

}

2 Comments

length is a NUMBER and should be checked with == 0. if(![string length]) is a c hack and (semantically) not correct since ! should be used for booleans. Please don't tell that it still works. I know that. It is just neanderthalish.
This answer is wrong. !0 is 1 too, just the same as 0 == 0. The result is the same, the only thing that can be argued about here is a matter of style

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.