I'm developing an iPhone app. In a label, I want to show an user's first letter of the name uppercase. How do I do that?
-
Where you you want to show it? In a textbox or a label or...? – Joachim Isaksson Jan 26 '12 at 17:11
-
5Have you checked the NSString documentation for some method that sounds like it returns a capitalized string? – UIAdam Jan 26 '12 at 17:11
-
If you're doing this to a last name, please keep in mind that capitalization rules of last names vary. Certain last names, like McDonald or O'Conor, have capital letters other than just the first. – Jay O'Conor Jan 26 '12 at 17:13
-
Well this is for userName and on the label. – HardCode Jan 26 '12 at 17:19
10 Answers
If there is only one word String, then use the method
let capitalizedString = myStr.capitalized // capitalizes every word
Otherwise, for multi word strings, you have to extract first character and make only that character upper case.

- 29,669
- 15
- 106
- 125
-
1as said below : this method capitalizes every word of the `NSString` – Matthieu Riegler May 23 '14 at 23:07
-
9"capitalizedString" Returns: A string with the first character from each word in the receiver changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values. – fullmoon Jun 27 '14 at 06:29
-
3If you have the string name of a database table named `yourCoolTable`, and you're after the name `YourCoolTable`, this won't work, because it returns `Yourcooltable`, which simply mangles your value in the opposite direction... fixing the first character, while trashing the rest of your characters. – Logicsaurus Rex Oct 24 '16 at 23:52
-
1Not an answer, because this solution converts the rest of the characters of the string to lowercase. – xpereta Jan 30 '17 at 16:42
(2014-07-24: Currently accepted answer is not correct) The question is very specific: Make the first letter uppercase, leave the rest lowercase. Using capitalizedString produces a different result: “Capitalized String” instead of “Capitalized string”. There is another variant depending on the locale, which is capitalizedStringWithLocale, but it's not correct for spanish, right now it's using the same rules as in english, so this is how I'm doing it for spanish:
NSString *abc = @"this is test";
abc = [NSString stringWithFormat:@"%@%@",[[abc substringToIndex:1] uppercaseString],[abc substringFromIndex:1] ];
NSLog(@"abc = %@",abc);

- 68,471
- 58
- 283
- 421

- 5,215
- 2
- 31
- 24
-
2
-
8Nope, Beryllium's answer capitalises everyword of a NSString. – Matthieu Riegler May 23 '14 at 23:08
-
4
In case someone is still interested in 2016, here is a Swift 3 extension:
extension String {
func capitalizedFirst() -> String {
let first = self[self.startIndex ..< self.index(startIndex, offsetBy: 1)]
let rest = self[self.index(startIndex, offsetBy: 1) ..< self.endIndex]
return first.uppercased() + rest.lowercased()
}
func capitalizedFirst(with: Locale?) -> String {
let first = self[self.startIndex ..< self.index(startIndex, offsetBy: 1)]
let rest = self[self.index(startIndex, offsetBy: 1) ..< self.endIndex]
return first.uppercased(with: with) + rest.lowercased(with: with)
}
}
Then you use it exactly as you would for the usual uppercased() or capitalized():
myString.capitalizedFirst()
or myString.capitalizedFirst(with: Locale.current)

- 3,324
- 3
- 19
- 21
This is for your NSString+Util
category...
- (NSString *) capitalizedFirstLetter {
NSString *retVal;
if (self.length < 2) {
retVal = self.capitalizedString;
} else {
retVal = string(@"%@%@",[[self substringToIndex:1] uppercaseString],[self substringFromIndex:1]);
}
return retVal;
}
You can do that with NSString stringWithFormat
, of course. I use this weirdness:
#define string(...) \
[NSString stringWithFormat:__VA_ARGS__]

- 68,471
- 58
- 283
- 421
-
There is no need to check for !self since your category method will not be called in that case. And the first three conditions can be replaced with if ( self.length <= 1 ) return self.capitalizedString for simplicity. – Peter N Lewis Feb 20 '15 at 05:48
-
You're right, but I think if the String got dealloc'ed it could be nil. Your point remains, about simplifying. Thanks – Dan Rosenstark Feb 20 '15 at 07:30
-
Even if the string was deallocated (which could only happen in a multithreaded context with erroneous memory management), self would remain a dangling pointer and not be nil. – Peter N Lewis Feb 20 '15 at 14:30
-
Simply
- (NSString *)capitalizeFirstLetterOnlyOfString:(NSString *)string{
NSMutableString *result = [string lowercaseString].mutableCopy;
[result replaceCharactersInRange:NSMakeRange(0, 1) withString:[[result substringToIndex:1] capitalizedString]];
return result;
}

- 2,937
- 5
- 36
- 56
As an extension to the accepted answer
capitalizedString is used for making uppercase letters .
NSString *capitalizedString = [myStr capitalizedString]; // capitalizes every word
But if you have many words in a string and wants to get only first character as upper case use the below solution
NSString *firstCapitalChar = [[string substringToIndex:1] capitalizedString];
NSString *capString = [string stringByReplacingCharactersInRange:NSMakeRange(0,1) withString: capString];
// extract first character and make only that character upper case.

- 5,773
- 4
- 52
- 64
here's a swift extension for it
extension NSString {
func capitalizeFirstLetter() -> NSString {
return self.length > 1 ?
self.substringToIndex(1).capitalizedString + self.substringFromIndex(1) :
self.capitalizedString
}
}

- 429
- 4
- 13
This is how it worked for me:
NSString *serverString = jsonObject[@"info"];
NSMutableString *textToDisplay = [NSMutableString stringWithFormat:@"%@", serverString];
[textToDisplay replaceCharactersInRange:NSMakeRange(0, 1) withString:[textToDisplay substringToIndex:1].capitalizedString];
cell.infoLabel.text = textToDisplay;
Hope it helps.

- 4,236
- 4
- 27
- 42
you can Just add this extension
extension String {
var capitalizedFirst: String {
self.prefix(1).capitalized + self.dropFirst().lowercased()
}
var capitalizedFirstWordInSentence: String {
let fullTextArr = self.components(separatedBy: " ")
var allText = ""
for i in 0..<fullTextArr.count {allText += fullTextArr[i].capitalizedFirst + " "}
allText.removeLast()
return allText
}
}
you can just use the first one only but the second one will convert every word to the first letter to be capitalized for example:
let hello = "hello, world!"
print(hello.capitalizedFirstWordInSentence)
//Hello,World!
print(hello.capitalizedFirst)
//Hello, world!

- 1
- 1
Swift:
let userName = "hard CODE"
yourLabel.text = userName.localizedUppercaseString
I recommend using this localised version of uppercase, since names are locale sensitive.

- 6,281
- 1
- 44
- 62