[英]How to best compare two NSString objects while ignoring case?
I want to compare two strings. 我想比较两个字符串。 It fails when the string have capital letter.
当字符串有大写字母时它会失败。 How do I convert both string to capitalize and compare.
如何将两个字符串转换为大写和比较。
I have a sample code, can someone correct this. 我有一个示例代码,有人可以纠正这个。
if ([[txtAnswer.text capitalizedString] isEqualToString:[answer capitalizedString]]) {
// Do somehing
}
如果查看NSString
类引用,您将在标题识别和比较字符串下看到方法caseInsensitiveCompare:
和localizedCaseInsensitiveCompare:
.
You might try something like: 您可以尝试以下方法:
if ([txtAnswer.text caseInsensitiveCompare: answer] == NSOrderedSame) {
// do something.
}
You can do a case insensitive string compare. 您可以进行不区分大小写的字符串比较。
if([txtAnswer.text compare:answer options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
// Do somehing
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.