Sai A Sai A
Updated date May 07, 2023
This blog explores various methods to convert strings to sentence case in C# and provides examples and explanations for each method. The blog aims to help developers manipulate strings effectively and improve the readability and professionalism of their code and output.

Introduction:

In programming, it is essential to manipulate strings effectively. One common task is converting strings to sentence case, where the first letter of each sentence is capitalized, and the rest are in lowercase. This is a useful formatting technique that can make the text more readable and professional. In this blog, we will discuss various methods to convert strings to sentence case in C#.

Method 1: Using ToTitleCase() Method

The easiest way to convert a string to a sentence case in C# is by using the ToTitleCase() method provided by the TextInfo class. This method converts the first character of each word to uppercase and the rest to lowercase. Here's an example:

string input = "hello world. this is a sentence.";
string output = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(input.ToLower());
Console.WriteLine(output);

Output:

Hello World. This Is A Sentence.

In this method, we first convert the input string to lowercase using the ToLower() method. Then we use the ToTitleCase() method of the TextInfo class to convert the first character of each word to uppercase.

Method 2: Using Regex

Another method to convert a string to a sentence case is by using regular expressions (regex). Regular expressions are a powerful tool for pattern matching and manipulation of strings. Here's an example:

string input = "hello world. this is a sentence.";
string output = Regex.Replace(input.ToLower(), @"(^\w)|(\.\s\w)", m => m.Value.ToUpper());
Console.WriteLine(output);

Output:

Hello World. This Is A Sentence.

In this method, we first convert the input string to lowercase using the ToLower() method. Then we use a regular expression to match the first word of the sentence and every word that follows a period and a space. The MatchEvaluator function then converts the matched characters to uppercase using the ToUpper() method.

Method 3: Using LINQ

LINQ (Language Integrated Query) is a powerful feature of C# that allows developers to query and manipulate data in a declarative way. We can use LINQ to convert a string to a sentence case. Here's an example:

string input = "hello world. this is a sentence.";
string[] words = input.ToLower().Split(' ');
string output = string.Join(" ", words.Select(w => char.ToUpper(w[0]) + w.Substring(1)));
Console.WriteLine(output);

Output:

Hello World. This Is A Sentence.

In this method, we first convert the input string to lowercase using the ToLower() method. Then we split the string into an array of words using the Split() method. Finally, we use LINQ to convert the first character of each word to uppercase and concatenate the words back into a single string using the Join() method.

Method 4: Using String Builder

String Builder is a class in C# that allows for efficient manipulation of strings. We can use the StringBuilder class to convert a string to a sentence case. Here's an example:

string input = "hello world. this is a sentence.";
StringBuilder sb = new StringBuilder(input.ToLower());
sb[0] = char.ToUpper(sb[0]);
for (int i = 1; i < sb.Length - 2; i++)
{
    if (sb[i] == '.' || sb[i] == '!' || sb[i] == '?')
    {
        sb[i + 2] = char.ToUpper(sb[i + 2]);
    }
}
string output = sb.ToString();
Console.WriteLine(output);

Output:

Hello World. This Is A Sentence.

In this method, we first convert the input string to lowercase and create a StringBuilder object from it. Then we convert the first character of the string to uppercase. We iterate over the string using a for loop and check if the current character is a period, exclamation mark, or question mark. If it is, we convert the next character to uppercase using the ToUpper() method. Finally, we convert the StringBuilder object to a string using the ToString() method.

Conclusion:

In conclusion, we have discussed various methods to convert strings to sentence case in C#. The first method uses the ToTitleCase() method provided by the TextInfo class. The second method uses regular expressions to match and convert characters to uppercase. The third method uses LINQ to convert the first character of each word to uppercase. The fourth method uses the StringBuilder class to manipulate the string efficiently.

Comments (1)

  • Bogdan P.
    Bogdan P. 16 Jan, 2024

    Are you serious? How is Title Case the same as Sentence Case? Does this look normal to you? "Hello World. This Is A Sentence." VERSUS "Hello world. This is a sentence." Please, guess which one is the correct sentence.