如何比较两个不区分大小写的字符串?

时间:2022-09-23 12:40:32

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");  
    }  
}  

5 个解决方案

#1


33  

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

如果您查看caseInsensitiveCompare:在文档中,您会看到它返回的是一个ns比较结果,而不是一个BOOL。在文档中查找,您会发现您可能希望它是NSOrderedSame。所以

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

如果([myString1 casein敏感度:myString2] == NSOrderedSame)

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

应该足够了。或者像罗伯特建议的那样比较小写的字符串。

#2


7  

Just use lowercaseString on both of the strings and then compare them as you would using a normal string equality check. It will still be O(n) so no big deal.

只需在两个字符串上使用小写字符串,然后像使用普通字符串相等检查那样对它们进行比较。还是O(n)所以没什么大不了的。

#3


4  

I would rather suggest to add a category on NSString:

我建议在NSString上添加一个类别:

- (BOOL)isEqualIgnoreCaseToString:(NSString *)iString {
    return ([self caseInsensitiveCompare:iString] == NSOrderedSame);
}

With this you can simply call:

有了这个,你可以简单地调用:

[myString1 isEqualIgnoreCaseToString:myString2];

#4


1  

To save a method call, I used a macro via a #define:

为了保存方法调用,我使用了一个宏通过#define:

#define isEqualIgnoreCaseToString(string1, string2) ([string1 caseInsensitiveCompare:string2] == NSOrderedSame)

Then call:

然后调用:

(BOOL) option = isEqualIgnoreCaseToString(compareString, toString);

#5


1  

A simple one, convert both strings in same case.Here i'm converting it to lower case and then checking it.

一个简单的,在相同的情况下转换两个字符串。我把它转换成小写,然后检查。

if ([[myString1 lowercaseString] [myString2 lowercaseString]])
{
 // same
}

#1


33  

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

如果您查看caseInsensitiveCompare:在文档中,您会看到它返回的是一个ns比较结果,而不是一个BOOL。在文档中查找,您会发现您可能希望它是NSOrderedSame。所以

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

如果([myString1 casein敏感度:myString2] == NSOrderedSame)

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

应该足够了。或者像罗伯特建议的那样比较小写的字符串。

#2


7  

Just use lowercaseString on both of the strings and then compare them as you would using a normal string equality check. It will still be O(n) so no big deal.

只需在两个字符串上使用小写字符串,然后像使用普通字符串相等检查那样对它们进行比较。还是O(n)所以没什么大不了的。

#3


4  

I would rather suggest to add a category on NSString:

我建议在NSString上添加一个类别:

- (BOOL)isEqualIgnoreCaseToString:(NSString *)iString {
    return ([self caseInsensitiveCompare:iString] == NSOrderedSame);
}

With this you can simply call:

有了这个,你可以简单地调用:

[myString1 isEqualIgnoreCaseToString:myString2];

#4


1  

To save a method call, I used a macro via a #define:

为了保存方法调用,我使用了一个宏通过#define:

#define isEqualIgnoreCaseToString(string1, string2) ([string1 caseInsensitiveCompare:string2] == NSOrderedSame)

Then call:

然后调用:

(BOOL) option = isEqualIgnoreCaseToString(compareString, toString);

#5


1  

A simple one, convert both strings in same case.Here i'm converting it to lower case and then checking it.

一个简单的,在相同的情况下转换两个字符串。我把它转换成小写,然后检查。

if ([[myString1 lowercaseString] [myString2 lowercaseString]])
{
 // same
}