如何比较两个不区分大小写的字符串? [英] How to compare two case insensitive strings?

查看:407
本文介绍了如何比较两个不区分大小写的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个包含相同字符串但大小写不同的字符串对象,现在我想在不区分大小写的情况下比较它们,如何实现?这是代码...

i have 2 string objects containing same string but case is different,now i wanna compare them ignoring the case sensitivity,how to do that??here is the code...

#import <Foundation/Foundation.h>
void main()  
{  
    NSString *myString1 = @"mphasis";  
    NSString *myString2 = @"MPHASIS";
    if ([myString1 caseInsenstiveCompare:myString2])  
    {  
        NSLog (@"ITS EQUAL");  
    }  
    else  
    {   
        NSLog (@"ITS NOT EQUAL");  
    }  
}  

推荐答案

如果您查找caseInsensitiveCompare:在文档中,您会发现您可能希望将其命名为NSOrderedSame.所以

If you look up caseInsensitiveCompare: in the docs you'll see that it returns an NSComparisonResult rather than a BOOL. Look that up in the docs and you'll see that you probably want it to be NSOrderedSame. So

if ([myString1 caseInsensitiveCompare:myString2] == NSOrderedSame)

应该可以解决问题.或者只是像罗伯特建议的那样比较小写字符串.

should do the trick. Or just compare the lowercase strings like Robert suggested.

这篇关于如何比较两个不区分大小写的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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