如何验证Objective C中的空字符串? [英] How to validate the empty string in Objective C?

查看:110
本文介绍了如何验证Objective C中的空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在获得下面的解析器委托方法后验证字符串值

I want to validate the string value after getting in the below parser delegate method

我试过像[string length]> 0,(string!= NULL)如果条件仍为空白字符串在NSlog中打印。那么验证sting的有效方法是什么。我使用了以下代码。

I have tried like [string length]>0 ,(string !=NULL) in if condition still blank string is printed in the NSlog.So what is the efficient method to validate the sting.I have used the below code.

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {   
    if ([elemName isEqualToString:@"productName"]) {
        if (!prodStringValue) {
            prodStringValue = [[NSMutableString alloc] initWithCapacity:50];
        }
        [prodStringValue appendString:string];
        if(prodStringValue && [prodStringValue length]>0 && (prodStringValue !=NULL))
        {
            prodNameStr = prodStringValue;
        NSLog(@"productName:%@",prodNameStr);
        }
        if(string && [string length]>0 && (string !=NULL))
        {
            prodNameStr = string;
            NSLog(@"productName:%@",string);
        }

    }
}


推荐答案

你可以在下面的几行代码中试试这个

you can try this below few lines of code

NSString *trimedstr  = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


if(trimedstr != [NSNull null]&& ![trimedstr isEqualToString:@""])
{
    if([trimedstr length]>0)
    {
        NSLog(@"%@", trimedstr);
    }
}

这篇关于如何验证Objective C中的空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆