Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uppercase first letter in NSString

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.

like image 796
Nielsou Hacken-Bergen Avatar asked Sep 09 '11 09:09

Nielsou Hacken-Bergen


People also ask

How do you capitalize the first letter of a variable in R?

Convert First letter of every word to Uppercase in R Programming – str_to_title() Function. str_to_title() Function in R Language is used to convert the first letter of every word of a string to Uppercase and the rest of the letters are converted to lower case.

How do you capitalize the first letter of a string in Objective C?

Just use the capitalizedString method. 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.

How do I type First Capital letters in TypeScript?

To capitalize the first letter of a string in TypeScript:Call the toUpperCase() method on the letter.

How do you capitalize the first letter in VB net?

In this function, we first test for null or empty parameters, and exit early in this case. Next We call ToCharArray to convert the String into a mutable array of characters. Then We use the Char. ToUpper method to uppercase the first letter, and we finally return a new String instance built from the character array.


1 Answers

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.

What if my string was nil?

Mr.Nil: I'm 'nil'. I can digest anything that you send to me. I won't allow your app to crash all by itself. ;)

Animation of a person swallowing fake explosives, it going off in his stomach, and then smoke coming from his mouth in a cartoonish fashion without injury.

nil will observe any method call you send to it.

So it will digest anything you try on it, nil is your friend.

like image 75
Vijay-Apple-Dev.blogspot.com Avatar answered Sep 28 '22 09:09

Vijay-Apple-Dev.blogspot.com