两个字符串的比较失败 [英] comparison of two strings fails
本文介绍了两个字符串的比较失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我比较两个明显相同的字符串。不幸的是,我的if子句不能按预期工作:
NSLog(@%@ ==%@?,strippedString1, strippedString2);
if(strippedString1 = strippedString2){
[_newBuild setTextColor:[NSColor greenColor]];
[_OldBuild setTextColor:[NSColor greenColor]];
}
这是我的NSLog输出:Build:2A12-046 == Build:2A12
即使逐个字节比较,两个字符串都是相同的:
解决方案
<用
==
比较指针地址,比较您可以使用的字符串的内容: [strippedString1 isEqualToString:strippedString2];
I make a comparison of two strings which are obviously identical. Unfortunately my if clause doesn´t work as expected:
NSLog(@"%@ == %@ ?",strippedString1,strippedString2);
if (strippedString1 == strippedString2) {
[_newBuild setTextColor: [NSColor greenColor] ];
[_OldBuild setTextColor: [NSColor greenColor] ];
}
This is my NSLog output: Build: 2A12-046 == Build: 2A12
Even a byte by byte comparison says both strings are identical:
Any clues ?
解决方案
With ==
you are comparing pointer address, to compare the contents of the strings you could use:
[strippedString1 isEqualToString: strippedString2];
这篇关于两个字符串的比较失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文