Easy way to set a single character of an NSString to uppercase
Solution 1
Since NSString
is immutable, what you have seems to be a good way to do what you want to do. The implementations of (NSString*)uppercaseString
and similar methods probably look very much like what you've written, as they return a new NSString
instead of modifying the one you sent the message to.
Solution 2
Very similar approach to what you have but a little more condense:
NSString *capitalisedSentence =
[dateString stringByReplacingCharactersInRange:NSMakeRange(0,1)
withString:[[dateString substringToIndex:1] capitalizedString]];
Solution 3
Aiming for maximum readability, make a category
on NSString
and give it this function:
NSString *capitalizeFirstLetter(NSString *string) {
NSString *firstCapital = [string substringToIndex:1].capitalizedString;
return [string stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:firstCapital];
}
Then in your code where you want it:
NSString *capitalizedSentence = capitalizeFirstLetter(dateString);
This kind of code rarely belongs in the spot where you need it and should generally be factored away into a utility class or a category
to improve legibility.
Solution 4
I had a similar requirement, but it was for characters within the string. This assuming i is your index to the character you want to uppercase this worked for me:
curword = [curword stringByReplacingCharactersInRange:NSMakeRange(i,1)
withString:[[curword substringWithRange:NSMakeRange(i, 1)] capitalizedString]];
Solution 5
If you profile these solutions they are much slower then doing this:
NSMutableString *capitolziedString = [NSMutableString stringWithString:originalString];
NSString *firstChar = [[capitolziedString substringWithRange:NSMakeRange(0,1)] uppercaseString];
[capitolziedString replaceCharactersInRange:NSMakeRange(0, 1) withString:firstChar];
in testing on an iphone 4 running iOS 5:
@doomspork's solution ran in 0.115750 ms
while above ran in 0.064250 ms;
in testing on an Simulator running iOS 5:
@doomspork's solution ran in 0.021232 ms
while above ran in 0.007495 ms;
Rog
iOS guy valiantly trying to wrestle JavaScript. Also available in short format: sockettrousers but that is really boring.
Updated on June 07, 2022Comments
-
Rog almost 2 years
I would like to change the first character of an NSString to uppercase. Unfortunately,
- (NSString *)capitalizedString
converts the first letter of every word to uppercase. Is there an easy way to convert just a single character to uppercase?I'm currently using:
NSRange firstCharRange = NSMakeRange(0,1); NSString* firstCharacter = [dateString substringWithRange:firstCharRange]; NSString* uppercaseFirstChar = [firstCharacter originalString]; NSMutableString* capitalisedSentence = [originalString mutableCopy]; [capitalisedSentence replaceCharactersInRange:firstCharRange withString:uppercaseFirstChar];
Which seems a little convoluted but at least makes no assumptions about the encoding of the underlying unicode string.
-
Rog almost 15 yearsyeah, optimised for cr/lf ;-) I like to let the optimiser take my temporaries out.
-
dwery about 13 yearsit seems that an underscore is considered a valid separator so foo_bar will be converted to "Foo_Bar"
-
ArtOfWarfare over 10 yearsIf this function were being run repeatedly it might be of concern that your solution runs in 1/3 to 1/2 the time, but as is, we're looking for the "easiest" solution, which could be read as the most legible.
-
odyth over 10 years@ArtOfWarfare My solution isn't that much harder to read then your's is and if your going to encapsulate the solution inside a category, that you wont probably ever look at again, then you should build the fastest solution so you don't have some weird performance problem later because you wrote inefficient code.