How can I uppercase the fisrt letter of a NSString, and removing any accents ?
For instance, Àlter
, Alter
, alter
should become Alter
.
But, /lter
, )lter
, :lter
should remains the same, as the first character is not a letter.
Please Do NOT use this method. Because one letter may have different count in different language. You can check dreamlax answer for that. But I'm sure that You would learn something from my answer. Happy coding :)
NSString *capitalisedSentence = nil;
//Does the string live in memory and does it have at least one letter?
if (yourString && yourString.length > 0) {
// Yes, it does.
capitalisedSentence = [yourString stringByReplacingCharactersInRange:NSMakeRange(0,1)
withString:[[yourString substringToIndex:1] capitalizedString]];
} else {
// No, it doesn't.
}
Why should I care about the number of letters?
If you try to access (e.g NSMakeRange
, substringToIndex
etc)
the first character in an empty string like @""
, then your app will crash. To avoid this you must verify that it exists before processing on it.
nil
will observe any method call you send to it.
So it will digest anything you try on it, nil
is your friend.