如何比较字符串? (`==`返回错误的值) [英] How to compare strings? (`==` returns wrong value)

查看:150
本文介绍了如何比较字符串? (`==`返回错误的值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经拥有了iPhone SDK的一部分,并且一直在尝试制作一些简单的应用程序。在这个,我想比较 self.label.string 的第一个字符与((UITextField *)sender).text的最后一个字符。 。我决定分别命名为 self.texty self.input

I've got myself a piece of the iPhone SDK and been trying to make some simple apps. In this one, i want to compare the first character of self.label.string with the last one of ((UITextField *)sender).text. I decided to name them self.texty and self.input, respectively.

在某些情况下,我希望这个if语句能够向我返回 yes ,但是我似乎无法完成这项工作。

I would expect this if statement returning yes to me under certain circumstances, however I can't seem to get that done.

(在我的情况下,我的 self.label.string 等于'你好!',而我的自我.input 以'h'结尾。)

(in my case, my self.label.string was equal to 'hello!', while my self.input ended in an 'h'.)

self.input = [NSString stringWithFormat:@"%@", [((UITextField *)sender).text substringFromIndex:[((UITextField *)sender).text length]-1]];
self.texty = [NSString stringWithFormat:@"%@", [self.label.string substringToIndex:1]];

if (self.input == self.texty) {
    return yes;
} else {
    return no;
}


推荐答案

字符串比较未完成 == ,但其中一个 NSString的比较方法

String comparison is not done with ==, but with one of the comparison methods of NSString.

例如:

if ([self.input compare:self.texty] == NSOrderedSame) {

这篇关于如何比较字符串? (`==`返回错误的值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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