Re: Reliable way to capitalize camel-case strings? [SOLVED]
Re: Reliable way to capitalize camel-case strings? [SOLVED]
- Subject: Re: Reliable way to capitalize camel-case strings? [SOLVED]
- From: Graham Cox <email@hidden>
- Date: Tue, 23 Dec 2008 00:25:20 +1100
On 22 Dec 2008, at 11:22 pm, Rob Rix wrote:
I’m interested to know if there’s a better way to do this than the
ugly way I’ve been using, too:
NSString *first = [string substringWithRange: NSMakeRange(0, 1)];
NSString *rest = [string substringFromIndex: 1];
NSString *result = [[first uppercaseString] stringByAppendingString:
rest];
Rob
On 22-Dec-08, at 6:23 AM, Graham Cox wrote:
In my app I need a way to generate the name of a method based on a
property key. If the key is, e.g. -scaleFactor, and the generated
method name needs to be -displayNameForScaleFactor, how can I
reliably turn "scaleFactor" into "ScaleFactor"? I tried [NSString
capitalizedString] but I get "Scalefactor". Obviously capitalizing
an ASCII string is trivial but I'm not sure that I can assume that
encoding for method names.
Since the KVC mechanism must be doing this a lot, I wondered if
there was an API I'm overlooking.
I ended up writing this category method. I guess it will be reliable,
as it's making no assumptions about encoding. There's clearly more
than one way to skin this cat... ;-)
- (NSString*) stringByCapitalizingFirstCharacter
{
// returns a copy of the receiver with just the first character
capitalized, ignoring all others.
// Thus, the rest of the string isn't necessarily forced to lowercase.
NSMutableString* sc = [self mutableCopy];
NSString* firstChar = [[self substringToIndex:1] uppercaseString];
[sc replaceCharactersInRange:NSMakeRange(0,1) withString:firstChar];
return [sc autorelease];
}
I'm calling this solved, though if anyone has a better method or knows
of an API, please pipe up!
thanks, Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden