Sai A Sai A
Updated date May 07, 2023
In this blog, we will learn to convert a string to proper case in C# using different methods such as the ToTitleCase method, the TextInfo class and the Split method, and regular expressions.
  • 2.2k
  • 0
  • 0

Introduction:

In many applications, we come across situations where we need to convert a string into a proper case. The proper case refers to capitalizing the first letter of each word in a string and making all other letters lowercase. Proper case conversion can be useful in various scenarios, such as displaying user names, formatting titles, and more. In this blog, we will discuss different methods to convert a string to the proper case in C#.

Method 1: Using the ToTitleCase method

The first and simplest method to convert a string to the proper case is to use the ToTitleCase method of the TextInfo class. This method takes a string as input and returns a new string with the first letter of each word capitalized and all other letters in lowercase.

Here is an example program that demonstrates the use of the ToTitleCase method:

using System;
using System.Globalization;

class Program {
  static void Main(string[] args) {
    string input = "hello world!";
    TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
    string output = ti.ToTitleCase(input);
    Console.WriteLine(output);
  }
}

Output:

Hello World!

In the above program, we first declare a string variable named input and initialize it with the value "hello world!". We then create an instance of the TextInfo class using the CurrentCulture property of the CultureInfo class. The TextInfo class provides methods for working with text, such as converting strings to different case formats.

Next, we call the ToTitleCase method of the TextInfo instance and pass in the input string as the argument. The ToTitleCase method returns a new string with the first letter of each word capitalized and all other letters in lowercase. We store this new string in the output variable.

Finally, we use the Console.WriteLine method to display the output string on the console, which is "Hello World!".

Method 2: Using the TextInfo class and Split method

The second method to convert a string to the proper case is to use the TextInfo class and the Split method of the string class. This method involves splitting the input string into individual words, capitalizing the first letter of each word, and then joining the words back into a single string.

Here is an example program that demonstrates this method:

using System;
using System.Globalization;

class Program {
  static void Main(string[] args) {
    string input = "hello world!";
    TextInfo ti = CultureInfo.CurrentCulture.TextInfo;
    string[] words = input.Split(' ');
    for (int i = 0; i < words.Length; i++) {
      words[i] = ti.ToTitleCase(words[i]);
    }
    string output = string.Join(" ", words);
    Console.WriteLine(output);
  }
}

Output:

Hello World!

In the above program, we first declare a string variable named input and initialize it with the value "hello world!". We then create an instance of the TextInfo class using the CurrentCulture property of the CultureInfo class.

Next, we split the input string into individual words using the Split method of the string class. The Split method takes a character delimiter as input and returns an array of strings. In this case, we split the input string on the space character ' '.

We then loop through each word in the words array and use the ToTitleCase method of the TextInfo instance to capitalize the first letter of each word. We store the result back into the words array.

Finally, we join the words back into a single string using the Join method of the string class. The Join method takes a separator string as input and returns a new string that consists of all the elements of an array separated by the separator string. In this case, we use the space character ' ' as the separator. We store the resulting string in the output variable and display it on the console using the Console.WriteLine method.

Method 3: Using regular expressions

The third method to convert a string to the proper case is to use regular expressions. Regular expressions are a powerful tool for pattern matching and text manipulation in C#. We can use regular expressions to find all the words in a string and capitalize the first letter of each word.

Here is an example program that demonstrates this method:

using System;
using System.Text.RegularExpressions;

class Program {
  static void Main(string[] args) {
    string input = "hello world!";
    string pattern = @"\b\w";
    string output = Regex.Replace(input, pattern, m => m.Value.ToUpper());
    Console.WriteLine(output);
  }
}

Output:

Hello World!

In the above program, we first declare a string variable named input and initialize it with the value "hello world!".

Next, we declare a string variable named pattern and initialize it with the regular expression @"\b\w". This regular expression matches the first letter of each word in a string. The \b character matches a word boundary, and the \w character matches a word character.

We then use the Regex.Replace method to replace each match of the pattern with the uppercase version of the match. The Regex.Replace method takes a string, a regular expression pattern, and a delegate function as input. The delegate function is called for each match of the pattern and returns the replacement value. In this case, we use a lambda expression to convert the match to uppercase using the ToUpper method of the string class.

Finally, we store the resulting string in the output variable and display it on the console using the Console.WriteLine method.

Conclusion:

In this blog, we have discussed different methods to convert a string to the proper case in C#. The first method uses the ToTitleCase method of the TextInfo class, the second method uses the TextInfo class and the Split method, and the third method uses regular expressions. Each method has its advantages and disadvantages, and the choice of method depends on the specific requirements of the application.

Comments (0)

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