简体   繁体   English

如何确定NSString是否全部大写

[英]How do I determine if an NSString is in all caps

How would I go about determining if a string is in all caps or not? 如何确定字符串是否全部大写?

I know you can check each individual character for being part of the upperCase character set, but is there a quicker way? 我知道您可以检查每个单独的字符是否属于upperCase字符集,但是有没有更快的方法?

You could try something like: 您可以尝试类似:

if ([[s uppercaseStringWithLocale:[NSLocale currentLocale]] isEqualToString:s])
{
    // ... s is uppercase ...
}
else
{
    // ... s is not all uppercase ...
}

There are a few different ways to interpret your question, and each of these examples just tests your string, without having to create a new copy: 有几种不同的方式来解释您的问题,每个示例仅测试您的字符串,而无需创建新副本:

1) You want to make sure that all letters that are in the string are in uppercase (but other characters, like punctuation, numbers, and white space are still allowed). 1)您要确保字符串中的所有字母均大写(但仍允许使用其他字符,例如标点符号,数字和空格)。 Example: 例:

NSCharacterSet *lowerCaseSet = [NSCharacterSet lowercaseLetterCharacterSet];

if ([myString rangeOfCharacterFromSet:lowerCaseSet].location == NSNotFound)
    NSLog(@"String is in upper case.");
else
    NSLog(@"String has invalid characters.");

2) You want to make sure that all of the characters in the string are uppercase characters (and no other characters are allowed). 2)您要确保字符串中的所有字符均为大写字符(不允许使用其他字符)。 Example: 例:

NSCharacterSet *invalidCharacterSet = [[NSCharacterSet uppercaseLetterCharacterSet] invertedSet];

if ([myString rangeOfCharacterFromSet:invalidCharacterSet].location == NSNotFound)
    NSLog(@"String has only upper case characters.");
else
    NSLog(@"String has invalid characters.");

3) You want to make sure that all of the characters in the string are uppercase characters or whitespace (and no other characters are allowed). 3)您要确保字符串中的所有字符均为大写字符或空格(并且不允许其他字符)。 Example: 例:

NSMutableCharacterSet *allowedCharacterSet = [NSMutableCharacterSet uppercaseLetterCharacterSet];
[allowedCharacterSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
NSCharacterSet *invalidCharacterSet = [allowedCharacterSet invertedSet];

if ([myString rangeOfCharacterFromSet:invalidCharacterSet].location == NSNotFound)
    NSLog(@"String has only upper case characters and/or white space.");
else
    NSLog(@"String has invalid characters.");

It depends a bit exactly what you're looking for. 这完全取决于您要查找的内容。

If you want to know if the string only contains upper case letters. 如果您想知道字符串是否仅包含大写字母。 I'd look for a regular expression match for [AZ]* and compare lengths. 我会寻找[AZ] *的正则表达式匹配项并比较长度。 I'm not sure how reliable [AZ]* is for non-english alphabets. 我不确定[AZ] *对于非英语字母的可靠性。

If you want to know if there are any lower case characters in the string, but you don't care about punctuation, user1118321's answer is good. 如果您想知道字符串中是否有任何小写字符,但是您不关心标点符号,则user1118321的答案很好。

Many good answers here, so let me just chip in a regular expression based one that works with letters from several national alphabets. 这里有很多很好的答案,所以让我简单地基于一个正则表达式,该正则表达式可以处理来自多个国家字母的字母。

Gordon Dove rightly mentioned that a traditional [AZ]* only matches the actual characters in the Latin/English alphabet from A to Z, and wouldn't work with for instance accented characters or other alphabets altogether. 戈登·多夫(Gordon Dove)正确地提到,传统的[AZ]*仅匹配拉丁字母/英文字母中从A到Z的实际字符,而不能与例如重音符号或其他字母一起使用。

So for the sake of completeness, here is the ICU regex that matches uppercase letters as defined by them: \\p{Lu} (a single uppercase letter). 因此,为了完整起见,下面是与它们定义的大写字母匹配的ICU正则表达式: \\p{Lu} (单个大写字母)。

To test it (with some western European characters, that I could type on my keyboard, the reader should feel free to add others): 要测试它(使用一些西欧字符,我可以在键盘上输入,读者可以随意添加其他字符):

NSString *lowercase = @"äbcdéfghìjklmñöpqrstûvwxyzæøå";
NSString *mixedcase = @"ÄbcDÉFGHïjkLmÑÖpqrstÛvwxyzÆøÅ";
NSString *uppercase = @"ÄBCDÉFGHÏJKLMÑÖPQRSTÛVWXYZÆØÅ";

NSArray *cases = @[lowercase, mixedcase, uppercase];

NSString *uppercasePattern = @"\\p{Lu}*"; // Letter, uppercase, zero or more occurrences

NSPredicate *ucPred = [NSPredicate predicateWithFormat:@"self MATCHES %@", uppercasePattern];

NSArray *ucArray = [cases filteredArrayUsingPredicate:ucPred];

If you just need to test a single string, you can use the predicate directly: 如果只需要测试单个字符串,则可以直接使用谓词:

if ( [ucPred evaluateWithObject:uppercase] )  {
    NSLog(@"It's uppercase");
}

You don't have to define the pattern string as a separate variable, but I tend to find it enhances readability of the code. 您不必将模式字符串定义为单独的变量,但是我倾向于发现它可以增强代码的可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM