xcode iOS 比较字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6936129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xcode iOS compare strings
提问by Tyler McMaster
How do i compare a website result with a predicted result.
我如何将网站结果与预测结果进行比较。
@"document.getElementsByTagName('body')[0].outerHTML"
is predicted to contain:
预计包含:
<body>OK</body>
But i always get an error saying that they don't match. I used this code below to compare them:
但我总是收到一个错误,说它们不匹配。我使用下面的代码来比较它们:
if (webresult == cmp){
then it shows an alert saying success. Or in else it'll say error. It always goes to else. Heres the code block, Please help.
然后它显示一个警告说成功。否则它会说错误。它总是去别的。这是代码块,请帮忙。
- (IBAction)displayresult:(id)sender {
webresult = [webview2 stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].outerHTML"];
NSString *cmp = [[NSString alloc] initWithFormat:@"<body>OK</body>"];
if (webresult == cmp) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Logged in" message:@"Logged in, Proceeding to the game" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:webresult delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
回答by msgambel
I assume that webresult
is an NSString
. If that is the case, then you want to use:
我假设那webresult
是一个NSString
. 如果是这种情况,那么您要使用:
if ([webresult isEqualToString:cmp]) {
instead of:
代替:
if (webresult == cmp) {
as the above method checks if the strings are equal character by character, whereas the bottom method checks if the two strings are the same pointer. Hope that helps!
上面的方法检查字符串是否一个字符相等,而底部方法检查两个字符串是否是相同的指针。希望有帮助!
回答by Mahesh
if (webresult == cmp)
Here, ==
checks whether webresult, cmp
are pointing to the same reference or not. You should instead compare value of the object by using NSString::isEqualToString.
在这里,==
检查是否webresult, cmp
指向相同的引用。您应该使用NSString::isEqualToString来比较对象的值。
if ( [ cmp isEqualToString:webresult ]) {
// ..
}else {
// ..
}
Note that isEqualToString
is a good option because it returns boolean value.
请注意,这isEqualToString
是一个不错的选择,因为它返回布尔值。
回答by Madhu
We cannot comapre the strings with ==
We have to use isEqualToString:
我们不能将字符串与 ==
我们必须使用isEqualToString:
if([str1 isEqualToString:str2])
{
}
else
{
}
回答by Jaw_khan
When comparing strings you need to use isEqualToString:
比较字符串时,您需要使用isEqualToString:
if ([cmp isEqualToString:webresult]) {
...
} else {
...
}
回答by Mehsam Saeed
for Swift 4.0:
对于 Swift 4.0:
if str1==str2 {
//both string are equal
}
else {
//do something expression not true
}
for Objective-C:
对于 Objective-C:
if ([str1 isEqualToString:str2]) {
//both string are equal
}
else {
//do something expression not true
}