Sentence Capitalization in C#

Given a sentence, capitalize the first letter of every word!

TextInfo.ToTitleCase()

We can use TextInfo.ToTitleCase() to solve the sentence capitalization problem. The TextInfo.ToTitleCase() converts the first character of a word to uppercase and the rest of the characters to lowercase. TextInfo.ToTitleCase() method belongs to System.Globalization namespace.

using System.Globalization;

 public static string Capitalize(string sentence)
 {
     return CultureInfo
         .CurrentCulture
         .TextInfo
         .ToTitleCase(sentence);
 }
Iterate over string

To capitalize the first letter of every word, we are going to start by creating an array of words by splitting the input sentence based on space character using String.Split() method. Each of those words is what we are going to capitalize. We need, also, an empty array to store the final capitalized words. We will then loop through the word array and uppercase the first letter of each word. We will combine that first letter that is now capitalized with the rest of the word and store the result in the new array. Finally, we will join the words array together into a string.

String.Substring() method retrieves a substring from a string instance. This method has two overloads. The first overload of String.Substring(int) retrieves a substring from the specified position to the end of the string. The second overload of String.Substring(int, int) method takes the starting character position and the length of characters in the returned substring.

public static string Capitalize(string sentence)
{
    var words = sentence.Split(' ');
    var builder = new string[words.Length];
    for (var i = 0; i < words.Length; i++)
    {
        var word = words[i];
        builder[i] = $"{word.Substring(0, 1).ToUpper()}{word.Substring(1).ToLower()}";
    }

    return string.Join(' ', builder);
}
C# 8.0: Range Operator

C# 8.0 added the range .. operator. The range operator provides a succinct syntax for accessing ranges in a sequence and makes a slice of the collection. Here, I am going to change the above code and use .. operator instead of String.Substring() method.

public static string Capitalize(string sentence)
{
    var words = sentence.Split(' ');
    var builder = new string[words.Length];
    for (var i = 0; i < words.Length; i++)
    {
        var word = words[i];
        builder[i] = $"{char.ToUpper(word[0])}{word[1..].ToLower()}";
    }

    return string.Join(' ', builder);
}
Use StringBuilder

Another solution for the sentence capitalization is using StringBuilder class instead of using an empty string array. We are then iterating through every character in the sentence. For each character, we will consider the character to the left of the current one. If it is a space, then we will take the current character that we are iterating over, capitalize it, and append it to the string represented by the StringBuilder. Otherwise, if it is not a space to the left of the current one, we will just take the character and append it to the string object.

For the first character, the above algorithm does not work well. We are capitalizing the first character before iterating through the sentence since the first character should always be capitalized. Then, the loop starts at the value of 1 instead of 0.

using System.Text;

 public static string Capitalize(string sentence)
 {
     var builder = new StringBuilder();
    
     // Capitalize the very first character
     builder.Append(char.ToUpper(sentence[0]));
    
     // Iterate from 1 to the length of the sentence
     for (var i = 1; i < sentence.Length; i++)
     {
         // Check the left character of the current character
         builder.Append(sentence[i - 1] == ' ' ?
             char.ToUpper(sentence[i]) :
             char.ToLower(sentence[i]));
     }

     return builder.ToString();
 }

You can download the source code on Github.