如何确定目标C中的字符串是否相等? [英] How to determine if strings are equal in Objective C?

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

问题描述

我从JSON结果中读取了一个字符串,如下所示:

I read a string from a JSON result as follows:

NSString *strResult = [[NSString alloc] initWithBytes:[data bytes] 
                                               length:[data length] 
                                             encoding:NSUTF8StringEncoding]; 

然后我尝试确定字符串是否等于值 N

I then try to determine if the string is equal to the value "N"

if ([strResult isEqualToString:@"N"])
{ 
  [lblImageOK setHidden:YES]; 
} 
else 
{ 
  [lblImageOk setHidden:NO]; 
}

if语句始终返回else部分,即使结果为 N 。它们都具有相同的值,但是语句始终返回false。

The if statement allways returns the else part, even though the result is "N". They both have the same value but the statement returns false always.

推荐答案

我找到了一种清除字符串然后检查是否有方法

I found a way to clean the string and then check if they are equal.

NSString *strResult = [[NSString alloc] initWithBytes:[data bytes] 
                                           length:[data length] 
                                         encoding:NSUTF8StringEncoding]; 

strResult = [strResult stringByReplacingOccurrencesOfString:@"\"" withString:@""];
strResult = [strResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

if ([strResult isEqualToString:@"N"])
{ 
  [lblImageOK setHidden:YES]; 
} 
else 
{ 
  [lblImageOk setHidden:NO]; 
}

这篇关于如何确定目标C中的字符串是否相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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