字符串比较失败 [英] String comparaison fail

查看:158
本文介绍了字符串比较失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ios开发的初学者,我正在尝试比较两个正常相等的字符串,但每次比较失败并转到其他地方:

I am a beginner in Ios developement and I'm trying to compare two string who are normaly equal but comparaison fail everytime and go to the else:

NSString *response = [session.channel execute:@"gpio read 0" error:&error];//execute SSH command and write this one in log

NSLog(response);
if ([response isEqualToString:(@"1")]) {
    [session.channel execute:@"gpio write 0 0" error:&error];
} else {
    [session.channel execute:@"gpio write 0 1" error:&error];
}

这是我的输出,响应等于1但不使用if :

here are my output, response is equal to 1 but the if is not used:


2014-04-08 20:01:38.065 domotiqueRPI [36948:1303] NMSSH:执行命令gpio read 0

2014-04-08 20:01:38.065 domotiqueRPI[36948:1303] NMSSH: Exec command gpio read 0

2014-04-08 20:01:38.108 domotiqueRPI [36948:60b] 1

2014-04-08 20:01:38.108 domotiqueRPI[36948:60b] 1

2014-04-08 20: 01:38.109 domotiqueRPI [36948:1303] NMSSH:执行命令gpio写0 1

2014-04-08 20:01:38.109 domotiqueRPI[36948:1303] NMSSH: Exec command gpio write 0 1

当然我在stackOverFlow上搜索但它告诉了它对我来说:是使用isEqualToString方法

Of course i search on stackOverFlow but all it tell to me : is to use isEqualToString method

推荐答案

您在回复结束时有换行符。试试这个:

You have a newline at the end of the response. Try this:

NSString *response = [session.channel execute:@"gpio read 0" error:&error];
response = [response stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

这将从响应字符串中删除尾随和前导空格/换行符。

This will remove trailing and leading whitespaces/newlines from the response string.

顺便说一句 - 不需要围绕 @1的括号。

BTW - no need for the parentheses around @"1".

这篇关于字符串比较失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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