How do I detect if a character is uppercase or lowercase?
Let's say I extract a single character from a NSString like this:
[@"This is my String" characterAtInd开发者_高级运维ex:0]
How do I find out if the character I get is a lowercase or uppercase character?
Thanks for any adivice!
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]];
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.
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.
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.
精彩评论