How do I test if a string is empty in Objective C?

2019-01-04 04:20发布

How do I test if an NSString is empty in Objective C?

30条回答
Animai°情兽
2楼-- · 2019-01-04 05:10

Another option is to check if it is equal to @"" with isEqualToString: like so:

if ([myString isEqualToString:@""]) {
    NSLog(@"myString IS empty!");
} else {
    NSLog(@"myString IS NOT empty, it is: %@", myString);
}
查看更多
趁早两清
3楼-- · 2019-01-04 05:10
if (string.length == 0) stringIsEmpty;
查看更多
Rolldiameter
4楼-- · 2019-01-04 05:11

So aside from the basic concept of checking for a string length less than 1, it is important to consider context deeply. Languages human or computer or otherwise might have different definitions of empty strings and within those same languages, additional context may further change the meaning.

Let's say empty string means "a string which does not contain any characters significant in the current context".

This could mean visually, as in color and background color are same in an attributed string. Effectively empty.

This could mean empty of meaningful characters. All dots or all dashes or all underscores might be considered empty. Further, empty of meaningful significant characters could mean a string that has no characters the reader understands. They could be characters in a language or characterSet defined as meaningless to the reader. We could define it a little differently to say the string forms no known words in a given language.

We could say empty is a function of the percentage of negative space in the glyphs rendered.

Even a sequence of non printable characters with no general visual representation is not truly empty. Control characters come to mind. Especially the low ASCII range (I'm surprised nobody mentioned those as they hose lots of systems and are not whitespace as they normally have no glyphs and no visual metrics). Yet the string length is not zero.

Conclusion. Length alone is not the only measure here. Contextual set membership is also pretty important.

Character Set membership is a very important common additional measure. Meaningful sequences are also a fairly common one. ( think SETI or crypto or captchas ) Additional more abstract context sets also exist.

So think carefully before assuming a string is only empty based on length or whitespace.

查看更多
可以哭但决不认输i
5楼-- · 2019-01-04 05:11

The best way in any case is to check the length of the given string.For this if your string is myString then the code is:

    int len = [myString length];
    if(len == 0){
       NSLog(@"String is empty");
    }
    else{
      NSLog(@"String is : %@", myString);
    }
查看更多
6楼-- · 2019-01-04 05:13

You can have an empty string in two ways:

1) @"" // Does not contain space

2) @" " // Contain Space

Technically both the strings are empty. We can write both the things just by using ONE Condition

if ([firstNameTF.text stringByReplacingOccurrencesOfString:@" " withString:@""].length==0)
{
    NSLog(@"Empty String");
}
else
{
    NSLog(@"String contains some value");
}
查看更多
smile是对你的礼貌
7楼-- · 2019-01-04 05:13

You can easily check if string is empty with this:

if ([yourstring isEqualToString:@""]) {
    // execute your action here if string is empty
}
查看更多
登录 后发表回答