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#
- Capitalize the First Letter of each Word in C#
- Capitalize the First Letter of each Word and Sentence in C#
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System; using System.Globalization; class Program { static void Main() { string input = "this is a sample sentence. this is another one. and here is a third."; string result = CapitalizeFirstLetterOfEachSentence(input); Console.WriteLine(result); } static string CapitalizeFirstLetterOfEachSentence(string input) { TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; string[] sentences = input.Split('.'); for (int i = 0; i < sentences.Length; i++) { sentences[i] = sentences[i].Trim(); if (!string.IsNullOrEmpty(sentences[i])) { sentences[i] = textInfo.ToTitleCase(sentences[i]); } } return string.Join(". ", sentences); } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System; using System.Globalization; class Program { static void Main() { string input = "this is a sample sentence. this is another one. and here is a third."; string result = CapitalizeFirstLetterOfEachWord(input); Console.WriteLine(result); } static string CapitalizeFirstLetterOfEachWord(string input) { TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; string[] words = input.Split(' '); for (int i = 0; i < words.Length; i++) { words[i] = textInfo.ToTitleCase(words[i]); } return string.Join(' ', words); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | using System; using System.Globalization; class Program { static void Main() { string input = "this is a sample sentence. this is another one. and here is a third."; string resultSentences = CapitalizeFirstLetterOfEachSentence(input); string resultWords = CapitalizeFirstLetterOfEachWord(input); Console.WriteLine("Original text: " + input); Console.WriteLine("Capitalized first letter of each sentence: " + resultSentences); Console.WriteLine("Capitalized first letter of each word: " + resultWords); } static string CapitalizeFirstLetterOfEachSentence(string input) { TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; string[] sentences = input.Split('.'); for (int i = 0; i < sentences.Length; i++) { sentences[i] = sentences[i].Trim(); if (!string.IsNullOrEmpty(sentences[i])) { sentences[i] = textInfo.ToTitleCase(sentences[i]); } } return string.Join(". ", sentences); } static string CapitalizeFirstLetterOfEachWord(string input) { TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; string[] words = input.Split(' '); for (int i = 0; i < words.Length; i++) { words[i] = textInfo.ToTitleCase(words[i]); } return string.Join(' ', words); } } |
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.