如何测试2个NSString以查看它们是否相同?

时间:2010-10-10 13:03:22

标签: objective-c nsstring

所以我想测试2 1NSString以查看它们在我输入时是否相同:

NSString *theOriginalString = [NSString stringWithFormat:@"Superman"];

NSString *theTypedString = [textView string];

我想查看TypedString输入时是否错误,以便在有人输入错误答案时弹出警告。

提前谢谢你。

1 个答案:

答案 0 :(得分:5)

使用isEqualToString:确定两个字符串是否相同,在这种情况下请执行以下操作:

if ([theOriginalString isEqualToString:theTypedString] == NO) {
    NSLog(@"The Strings are Different, wrong answer!");
} else {
    NSLog(@"The Strings are the Same, correct answer!");
}

修改

如果你想确定他们到目前为止输入的内容是对的,试试这个:

if ([theOriginalString hasPrefix:theTypedString] == NO) {
    NSLog(@"The Strings are Different, wrong answer!");
} else {
    NSLog(@"The Strings are the Same, correct answer!");
}