ExtensionMethod.NET Home of 817 C#, Visual Basic, F# and Javascript extension methods

UcWords

Emulates PHPs ucwords - capitalize each word

Source

public static StringBuilder UcWords(this string theString)
{
    StringBuilder output = new StringBuilder();
    string[] pieces = theString.Split(' ');
    foreach (string piece in pieces)
    {
        char[] theChars = piece.ToCharArray();
        theChars[0] = char.ToUpper(theChars[0]);
        output.Append(' ');
        output.Append(new string(theChars));
    }

    return output;

}

Example

"a sentence of words".UcWords();

string str = "some words to capitalize";
str.UcWords();

Author: Stuart Sillitoe

Submitted on: 1 jun 2016

Language: C#

Type: String

Views: 2937