2

Hey! I was wondering if there was any way to check if the first letter of a string was capital or not in an NSString. Something similar to:

if ([[string substringToIndex:1] isCapitalLetter]) {
    // CODE
}

--or--

if ([self isCapitalLetter:[string substringToIndex:1]]) {
    // CODE
}
Hank Brekke
  • 2,024
  • 2
  • 24
  • 33

2 Answers2

18

[[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[myString characterAtIndex:0]];

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
2

The only thing I can think of would be to do something like this:

// get the first character, capitalized
NSString *capital = [[oldstring substringToIndex:1] capitalizedString];

// then compare to your oldstring
if ( [[oldstring substringToIndex:1] isEqualToString:capital] ) {
    // do stuff...
}

The NSString reference is your friend: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

livingtech
  • 3,570
  • 29
  • 42