Need a simple CRM and Project Management system?
Check out JobNimbus - CRM for Contractors and Service Professionals.
Check out JobNimbus - CRM for Contractors and Service Professionals.
C# Capitalize First Letters of Words
When gathering user input, especially in a free form text box, users will enter something like their first and last name but will not capitalize the first letters for the name. Then later, you may want to display the name and you find that it doesn't look very nice and is harder to read because of lack of capitalization.
The below code will take a value such as "john doe" and return it as the string "John Doe". You can even use the below code to format something like a blog title where you want each word to be capitalized.
/// <summary> /// Sets the first letter of each word in the value specified to /// capitalized. /// </summary> /// <param name="sValue">The word(s) to capitalize the first /// letter of each.</param> public static string CapitalizeFirstLetters(string sValue) { char[] array = sValue.ToCharArray(); // handle the first letter in the string if (array.Length >= 1) { if (char.IsLower(array[0])) { array[0] = char.ToUpper(array[0]); } } // scan through the letters, checking for spaces for (int i = 1; i < array.Length; i++) { if (array[i - 1] == ' ') { if (char.IsLower(array[i])) { array[i] = char.ToUpper(array[i]); } } } return new string(array); }
Popular Articles
Recent comments
- Reply to comment - Code Samples & Tutorials
6 hours 54 min ago - thank you for sharing
20 hours 13 min ago - Great explanation and more questions
1 day 23 hours ago - Insertion of illegal Element:
4 weeks 4 days ago - Insertion of illegal Element: 32
4 weeks 4 days ago - re "But, this will NOT work."
5 weeks 5 days ago - Unable to cast COM object of t
5 weeks 5 days ago - Saved my life
5 weeks 6 days ago - nice
8 weeks 5 days ago - good article
9 weeks 6 days ago

Had A Bug In Your Code
Its Nice And Good...But It Has A Bug When We Want To Insert/Type A Letter Or Word In Between The Typed Word.Then The Cursor Control Is Always Jumps To End Of The Word..
C# Capitalize First Letters of Words
string word ="demo thong tin";
string capLetter = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word.ToLower())
result:
capLetter = "Demo Thong Tin"
Had A Bug In Your Code
Its Nice And Good...But It Has A Bug When We Want To Insert/Type A Letter Or Word In Between The Typed Word.Then The Cursor Control Is Always Jumps To End Of The Word..