Capitalize Sentences and Words – C# Programming Examples

Capitalize Sentences and Words

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:

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:

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.

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.

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post