不区分大小写的 NSString 比较

发布于 2024-10-08 03:08:37 字数 128 浏览 7 评论 0原文

使用此代码我可以比较字符串值。

[elementName isEqualToString: @"Response"]

但这是区分大小写的。有没有办法比较字符串而不区分大小写?

Using this code I am able to compare string values.

[elementName isEqualToString: @"Response"]

But this compares case-sensitively. Is there a way to compare the string without case sensitivity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绳情 2024-10-15 03:08:37

NSString 上有一个 caseInsensitiveCompare: 方法,为什么不阅读 文档?该方法返回 NSComparisonResult:

enum {
   NSOrderedAscending = -1,
   NSOrderedSame,
   NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

…啊,抱歉,刚才我意识到您要求区分大小写相等。 (为什么我不阅读这个问题?:-)默认的 isEqual:isEqualToString: 相等性应该已经区分大小写,这是什么情况?

There’s a caseInsensitiveCompare: method on NSString, why don’t you read the documentation? The method returns NSComparisonResult:

enum {
   NSOrderedAscending = -1,
   NSOrderedSame,
   NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

…ah, sorry, just now I realized you are asking for case sensitive equality. (Why don’t I read the question? :-) The default isEqual: or isEqualToString: equality should already be case sensitive, what gives?

許願樹丅啲祈禱 2024-10-15 03:08:37

以下是您需要比较字符串而不关心它是小写还是大写的代码:

if ([elementName caseInsensitiveCompare:@"Response"]==NSOrderedSame)
{
    //  Your "elementName" variable IS "Response", "response", "reSPonse", etc
    //  
}

Here's the code you would need to compare a string without caring about whether it's lowercase or uppercase:

if ([elementName caseInsensitiveCompare:@"Response"]==NSOrderedSame)
{
    //  Your "elementName" variable IS "Response", "response", "reSPonse", etc
    //  
}
红墙和绿瓦 2024-10-15 03:08:37

实际上 isEqualToString: 具有区分大小写的能力。
as:

[elementName isEqualToString: @"Response"];

如果您想要求不区分大小写的比较,那么这里是代码:

您可以将两个可比字符串更改为小写或大写,并且可以比较为:

NSString *tempString = @"Response";
NSString *string1 = [elementName lowercaseString];
NSString *string2 =  [tempString lowercaseString];

//The same code changes both strings in lowerCase.
//Now You Can compare

if([string1 isEqualToString:string2])
{

//Type your code here

}

Actually isEqualToString: works with case sensitive ability.
as:

[elementName isEqualToString: @"Response"];

if you want to ask for case insensitive compare then here is the code:

You can change both comparable string to lowerCase or uppercase, and can compare as:

NSString *tempString = @"Response";
NSString *string1 = [elementName lowercaseString];
NSString *string2 =  [tempString lowercaseString];

//The same code changes both strings in lowerCase.
//Now You Can compare

if([string1 isEqualToString:string2])
{

//Type your code here

}
温馨耳语 2024-10-15 03:08:37
NSString *string1 = @"stringABC";
NSString *string2 = @"STRINGDEF";
NSComparisonResult result = [string1 caseInsensitiveCompare:string2];

if (result == NSOrderedAscending) {
  NSLog(@"string1 comes before string2");
} else if (result == NSOrderedSame) {
  NSLog(@"We're comparing the same string");
} else if (result == NSOrderedDescending) {
   NSLog(@"string2 comes before string1");
}
NSString *string1 = @"stringABC";
NSString *string2 = @"STRINGDEF";
NSComparisonResult result = [string1 caseInsensitiveCompare:string2];

if (result == NSOrderedAscending) {
  NSLog(@"string1 comes before string2");
} else if (result == NSOrderedSame) {
  NSLog(@"We're comparing the same string");
} else if (result == NSOrderedDescending) {
   NSLog(@"string2 comes before string1");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文