Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I capitalize first letter of first name and last name in C#?

Is there an easy way to capitalize the first letter of a string and lower the rest of it? Is there a built in method or do I need to make my own?

like image 737
Mike Roosa Avatar asked Sep 16 '08 14:09

Mike Roosa


People also ask

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

h> int toupper(int c); int tolower(int c); DESCRIPTION toupper() converts the letter c to upper case, if possible. tolower() converts the letter c to lower case, if possible.

How do you capitalize the first and last letter of a string?

Use list slicing and str. upper() to capitalize the first letter of the string. Use str. join() to combine the capitalized first letter with the rest of the characters.

Do you capitalize both first and last name?

People's names are proper nouns, and therefore should be capitalized. The first letter of someone's first, middle, and last name is always capitalized, as in John William Smith. Take note that some non-English surnames may begin with lowercase letters, such as Vincent van Gogh or Leonardo da Vinci.

Is C sharp capitalize First letter?

In C#, the Toupper() function of the char class converts a character into uppercase. In the case that we will be discussing, only the first character of the string needs to be converted to uppercase; the rest of the string will stay as it is.


1 Answers

TextInfo.ToTitleCase() capitalizes the first character in each token of a string.
If there is no need to maintain Acronym Uppercasing, then you should include ToLower().

string s = "JOHN DOE"; s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToLower()); // Produces "John Doe" 

If CurrentCulture is unavailable, use:

string s = "JOHN DOE"; s = new System.Globalization.CultureInfo("en-US", false).TextInfo.ToTitleCase(s.ToLower()); 

See the MSDN Link for a detailed description.

like image 71
ageektrapped Avatar answered Sep 19 '22 14:09

ageektrapped