Mohanapriya R Mohanapriya R
Updated date Apr 27, 2024
In this article, we will learn how to convert strings to sentence case in C#.

Convert Strings to Sentence Case in C#

Sentence case involves capitalizing the first letter of each sentence while keeping the rest of the letters in lowercase. This is different from the title case, where the first letter of every word is capitalized. Sentence case is often used for readability and consistency in written communication.

using System;
using System.Globalization;

class Program
{
    static string ConvertToSentenceCase(string input)
    {
        TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
        return textInfo.ToTitleCase(input.ToLower());
    }

    static void Main()
    {
        Console.WriteLine("Enter a string:");
        string userInput = Console.ReadLine();

        string sentenceCaseString = ConvertToSentenceCase(userInput);

        Console.WriteLine("\nOriginal String: " + userInput);
        Console.WriteLine("Sentence Case: " + sentenceCaseString);
    }
}
  • The ConvertToSentenceCase method takes a string as input, converts it to lowercase, and then uses the TextInfo.ToTitleCase method to capitalize the first letter of each sentence.
  • In the Main method, the user is prompted to enter a string, and the ConvertToSentenceCase method is called to process the input.
  • The original and sentence-cased strings are then displayed as output.

Output:

Enter a string:
hello world. this is a sample input.

Original String: hello world. this is a sample input.
Sentence Case: Hello world. This is a sample input.

Comments (0)

There are no comments. Be the first to comment!!!