views:

1445

answers:

4

Hi, I'm working with cocoa on the iPhone and I'm looking for some method like:

NSString *s = @"Hello";

[s isStringStartsWithUpperCaseCharacter]

-(BOOL) isStringStartsWithUpperCaseCharacter;

The first letter of the string may be non ASCII letter like: Á, Ç, Ê...

Is there some method which can helps me? I saw in the documentation that there are some methods to convert the string to uppercase and to lowercase but there are no methods for ask if the string is lowercased or uppercased. Thank you

A: 

I don't know Objective-C yet but you can put the first character in another string and "toupper" it... then compare it to the first character of the original string.. if they're equal, the first character is upper case.

Mike Gleason jr Couturier
Please downvote answers that are wrong or not useful. This answer is none of that.
Nikolai Ruhe
Hi Nikolai, I assume I had 2 votes up!? Thanks for the comment. Maybe I should have stayed away from a language I'm learning for 2 days anyway :)
Mike Gleason jr Couturier
I wrote the comment when the answer was rated -1. Congratulations on your decision to learn the beautiful language/framework combination of Objective-C/Cocoa.
Nikolai Ruhe
+1  A: 
return [myString rangeOfCharacterFromSet: [NSCharacterSet uppercaseLetterCharacterSet]].location==0;
+8  A: 
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]];

Edit:

I'm not sure what's the difference between uppercaseLetterCharacterSet and capitalizedLetterCharacterSet. If someone finds out, please leave a comment!

2nd Edit:

Thanks, Ole Begemann, for finding out about the differences. I edited the code to make it work as expected.

Nikolai Ruhe
In Unicode, letters can belong to different categories, such "Lowercase", "Uppercase", or "Titlecase". The difference between Uppercase (LIKE THIS) and Titlecase (Like This) is important as some languages use different forms for some letters. According to the docs, `+capitalizedLetterCharacterSet` contains the Titlecase category, whereas `+uppercaseLetterCharacterSet` contains Uppercase and Titlecase. So the latter is a superset of the former (the differences between the two are probably small). The OP must decide which one is appropriate. (Source: http://unicode.org/reports/tr21/tr21-3.html)
Ole Begemann
Correction: the differences between the two are huge. I just wrote a small test program to check. `+capitalizedCharacterSet` only contains these 31 (obscure) characters: DžLjNjDzᾈᾉᾊᾋᾌᾍᾎᾏᾘᾙᾚᾛᾜᾝᾞᾟᾨᾩᾪᾫᾬᾭᾮᾯᾼῌῼ (I hope these display correctly)Whereas `+uppercaseLetterCharacterSet` contains 968 characters in various languages, including the latin alphabet in uppercase. So this is the set to use.
Ole Begemann
I should add I checked all characters between 0 and UINT16_MAX, so the numbers above do not include UTF-32 characters.
Ole Begemann
A: 

first of all, if you just want to make the first character capitalized, try

- (NSString *)capitalizedString;

otherwise, you can use something like

NSString *firstCharacter = [s substringWithRange:NSMakeRange(0,1)];
if (firstCharacter != nil && [firstCharacter isEqualToString:[firstCharacter uppercaseString]]) {
//first character was capitalized
} else {
//first character was lowercase
}
Kenny Winker
Note that capitalizedString will ALSO make all other characters in each word lowercase. So if you have a word like capitalizedString, it will end up as Capitalizedstring... often not what you really want.
Kendall Helmstetter Gelner