Need a simple CRM and Project Management system?
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);
}

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..