8

Let's say I extract a single character from a NSString like this:

[@"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!

Funkybit
  • 359
  • 1
  • 2
  • 15

4 Answers4

34
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]];
Simon Lee
  • 22,304
  • 4
  • 41
  • 45
  • 10
    +1 Probably worth noting that `!isUppercase` is not equivalent to `isLowercase`. – walkytalky Jun 15 '11 at 10:18
  • Indeed, you can do the reverse with lowercaseLetterCharacterSet etc. – Simon Lee Jun 15 '11 at 10:22
  • I have not tested this, but logic tells me that the uppercaseLetterCharacterSet is a collection of all the uppercase characters. Numbers and other non-lowercase characters would return turn if you test !isUppercase – Armand Jan 10 '17 at 05:17
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.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • @taskinoor Good question. This is limited but seems a perfectly fine solution if the constraints are met. – walkytalky Jun 15 '11 at 10:23
  • 1
    This also works, just testet. But yes, I need it for german character sets, so this check is a bit to limited. Anyway thanks for the advice! – Funkybit Jun 15 '11 at 10:25
  • 2
    @Funkybit, you should have mentioned that in question :-P – taskinoor Jun 15 '11 at 10:29
  • @walkytalky, I understand the limitation and mentioned that in the answer. And it was not mentioned in the question that German is needed. – taskinoor Jun 15 '11 at 10:32
  • @taskinoor Indeed, that's what I was trying to say -- I too think the downvote was strange. I had already +1-ed. – walkytalky Jun 15 '11 at 10:37
  • This is limited to the English ASCII. It will not work with accented characters, or non-latin characters. Probably the reason for the down vote. – Armand Jan 10 '17 at 05:14
2
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.

neubert
  • 15,947
  • 24
  • 120
  • 212
hoang21
  • 133
  • 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.

Mick Sear
  • 1,549
  • 15
  • 25
  • 3
    -1 Sorry, but this is really an example of trying to use a hammer to unscrew a bolt. – walkytalky Jun 15 '11 at 10:14
  • But it would work. Isn't downvoting for things that just don't work? – Mick Sear Jun 15 '11 at 10:23
  • 4
    Downvoting is for bad answers. "I don't know how to do this natively but please drag in some inappropriate technology that isn't normally present or convenient to use and then do a bunch more unnecessary work to get an only barely-useful result." is a bad answer. – walkytalky Jun 15 '11 at 10:28
  • This was helpful to me as I was trying to detect acronyms from just capitalized words or letters. – nickthedude Sep 22 '15 at 00:07