Let's say I extract a single character from a NSString like this:
假设我从NSString中提取单个字符,如下所示:
[@"This is my String" characterAtIndex:0]
How do I find out if the character I get is a lowercase or uppercase character?
如何确定我得到的字符是小写还是大写字母?
Thanks for any adivice!
感谢您的任何帮助!
4 个解决方案
#1
33
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]];
#2
5
unichar ch = [@"This is my String" characterAtIndex:0];
if (ch >= 'A' && ch <= 'Z') {
// upper case
} else if (ch >= 'a' && ch <= 'z') {
// lower case
}
Note that this works only for English alphabet.
请注意,这仅适用于英文字母。
#3
1
NSString *firstChar = [NSString stringWithFormat:@"%c", [@"This is my String" characterAtIndex:0]];
BOOL isUpperCase = [firstChar isEqualToString:[firstChar uppercaseString]];
Note that, if the character doesn't have uppercase/lowercase variants (like number 1, 2, etc), the isUpperCase always return YES.
请注意,如果字符没有大写/小写变体(如数字1,2等),则isUpperCase始终返回YES。
#4
-1
I don't know objective C, but you could use a regular expression. If it matches [a-z], then it's lower case. If it matches [A-Z], it's upper case.
我不知道目标C,但你可以使用正则表达式。如果它匹配[a-z],那么它是小写的。如果它与[A-Z]匹配,则为大写。
#1
33
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]];
#2
5
unichar ch = [@"This is my String" characterAtIndex:0];
if (ch >= 'A' && ch <= 'Z') {
// upper case
} else if (ch >= 'a' && ch <= 'z') {
// lower case
}
Note that this works only for English alphabet.
请注意,这仅适用于英文字母。
#3
1
NSString *firstChar = [NSString stringWithFormat:@"%c", [@"This is my String" characterAtIndex:0]];
BOOL isUpperCase = [firstChar isEqualToString:[firstChar uppercaseString]];
Note that, if the character doesn't have uppercase/lowercase variants (like number 1, 2, etc), the isUpperCase always return YES.
请注意,如果字符没有大写/小写变体(如数字1,2等),则isUpperCase始终返回YES。
#4
-1
I don't know objective C, but you could use a regular expression. If it matches [a-z], then it's lower case. If it matches [A-Z], it's upper case.
我不知道目标C,但你可以使用正则表达式。如果它匹配[a-z],那么它是小写的。如果它与[A-Z]匹配,则为大写。