search

Home  >  Q&A  >  body text

objective-c - 如何检测字符串在Obj-C里是否为空?

如何检测NSString在Objective-C里是否为空?

原问题:How do I test if a string is empty in Objective C?

大家讲道理大家讲道理2868 days ago533

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-21 11:20:49

    Marc Charbonneau
    You can check if [string length] == 0. This will check if it is a valid but empty string (@"") and if it is null. Because calling length with no value will also return 0.


    Matt G
    The answer above is correct. But I would like to take this opportunity to introduce isEmpty summarized by Wil Shipley, which he shared on his blog:

    static inline BOOL IsEmpty(id thing) {
    return thing == nil
    || ([thing respondsToSelector:@selector(length)]
    && [(NSData *)thing length] == 0)
    || ([thing respondsToSelector:@selector(count)]
    && [(NSArray *)thing count] == 0);
    }
    

    The first method is effective, but it will not work if the string has spaces (@" "). So the spaces must be cleared before testing.
    The following code can remove all spaces on both sides of the string

    [stringObject stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ];
    

    A good way is to define a macro so that you don’t have to enter this long string of code:

    #define allTrim( object ) [object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ]
    

    Now you can use:

    NSString *emptyString = @"   ";
    if ( [allTrim( emptyString ) length] == 0 ) NSLog(@"Is empty!");
    

    Rob
    One of the best workarounds I've seen (better than Matt G's) is this improved inline function, which I found in a Git Hub report:

    // Check if the "thing" pass'd is empty
    static inline BOOL isEmpty(id thing) {
        return thing == nil
        || [thing isKindOfClass:[NSNull class]]
        || ([thing respondsToSelector:@selector(length)]
            && [(NSData *)thing length] == 0)
        || ([thing respondsToSelector:@selector(count)]
            && [(NSArray *)thing count] == 0);
    }
    

    user238824
    You'd better use this function:

    @implementation NSString (Empty)
    
        - (BOOL) empty{
            return ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]length] == 0);
        }
    
    @end
    

    chown
    Another option is to use isEqualToString to check if it is equal to @"", like this:

    if ([myString isEqualToString:@""]) {
        NSLog(@"myString IS empty!");
    } else {
        NSLog(@"myString IS NOT empty, it is: %@", myString);
    }
    

    Jim Thio
    This one I use:

    @implementation NSObject (AdditionalMethod)
    -(BOOL) isNotEmpty
    {
        return !(self == nil
        || [self isKindOfClass:[NSNull class]]
        || ([self respondsToSelector:@selector(length)]
            && [(NSData *)self length] == 0)
        || ([self respondsToSelector:@selector(count)]
            && [(NSArray *)self count] == 0));
    
    };
    @end 
    

    The problem is that if self has no value, this function will never be called. It will return false, which is required.


    Samir Jwarchan
    Just use one of the following if else conditions:
    Method 1:

    <strong>if([yourString isEqualToString:@""]){
            // yourString is empty.
        }
        else {
            // yourString has some text on it.
        } </strong>
    

    Method 2:

    if([yourString length] == 0){
        // Empty yourString
    }
    else {
        // yourString is not empty
    }
    

    reply
    0
  • Cancelreply