tags:

views:

823

answers:

3

Hello,

I am having a NSString to which I am trimming at the start and end for blank spaces. Now I want to make the first alphabet or letter of the string to appear as capital.

Is there any short available or I need to take the first alphabet and convert it to capital on my own.

tnx.

A: 
- (NSString *)capitalizedString

will make all words in the string initially capitalized. If you only have one or two words in your string, this may be what you want.

Here is the NSString class reference.

Brian Ramsay
No I just want the first alphabet to be capital and not the whole string or the complete words.
rkb
A: 

What I always used to do in C, which still work fine, is to convert the first char to an int (ASCII representation), add the difference between caps and non-caps (-32) and then cast back to a char. You would then just need to assign that to be your first char.

TahoeWolverine
This fails if the letter is already capitalized though.
Dan Lorenc
It's pretty trivial how to solve that though. Test for values between 97 and 122 (lower-case) and only modify in that range.
TahoeWolverine
unless you're absolutely sure that your string is in English you probably really don't want to be doing this sort of thing... people all over the world will be downloading your app from the App Store!
David Maymudes
+3  A: 

You have to do it by hand. This is one of those things that has been built into categories so often by so many people that Apple probably doesn't dare add it as an actual method since it would generate category collisions.

NSString *firstCapChar = [[string substringToIndex:1] capitalizedString];
NSString *cappedString = [string stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];

There are of course many other solutions using NSMutableString or any other way you like to do it using unichar or whatever.

Rob Napier
Amazing dude, it works, thts wht I want.
rkb