In C# programming, text manipulation is essential for creating clean and readable content. Whether you’re dealing with user input, processing text files, or enhancing the presentation of your application, understanding how to capitalize sentences and words plays a crucial role.

In this article, we will show you practical C# examples that showcase techniques for capitalizing the first letter of each sentence and each word.

Table of Contents

Capitalize the First Letter of each Sentence in C#

This is C# function that will capitalize the first letter of each sentence in the the input string. you can use the TextInfo class from the System.Globalization namespace to capitalize the first letter of each sentence. Here’s an example:

This example defines a method CapitalizeFirstLetterOfEachSentence that takes a string as input, splits it into sentences using the period as a delimiter, capitalizes the first letter of each sentence, and then joins the sentences back together. The TextInfo.ToTitleCase method is used to capitalize the first letter of each word in a sentence.

Capitalize the First Letter of each Word in C#

Similarly you can modify this example to capitalize the first letter of each word in the input text, you can see the modified example as follows:

In this modified example, the CapitalizeFirstLetterOfEachWord method splits the input text into words using space as a delimiter, and then it capitalizes the first letter of each word using the TextInfo.ToTitleCase method. Finally, it joins the words back together into a string.

Capitalize the First Letter of each Word and Sentence in C#

Now you can combined both these examples together to capitalize first letter of each word and sentence in the given text.

In this example, the Main method calls both CapitalizeFirstLetterOfEachSentence and CapitalizeFirstLetterOfEachWord to demonstrate capitalization at both the sentence and word levels. The input text is displayed, along with the results of the two capitalization methods.