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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With