开发者

How do I detect if a character is uppercase or lowercase?

开发者 https://www.devze.com 2023-03-12 22:30 出处:网络
Let\'s say I extract a single character from a NSString like this: [@\"This is my String\" characterAtInd开发者_高级运维ex:0]

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消