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?
Solution 1:
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.
Solution 2:
CultureInfo.CurrentCulture.TextInfo.ToTitleCase("hello world");