This post will discuss how to convert the first letter of a string to uppercase in C#. In order words, capitalize a string in C#.

1. Using String.Substring() method

The idea is to extract the first letter from the string and convert it to uppercase by invoking the ToUpper() method, and concatenate the uppercase character with the remaining string. The following sample illustrates this.

Download  Run Code

 
Don’t forget to check for the empty string, otherwise IndexOutOfRangeException will be thrown. If you need to convert the first letter of a string to uppercase and the remaining characters to lower case, you can do something like:

Download  Run Code

2. Using AsSpan() method

Alternately, you can use the AsSpan() method over Substring() method. It creates a new read-only span over a string, starting from the specified position till its end.

Download  Run Code

3. Using TextInfo.ToTitleCase() method

If you need to convert your string to a title case, you can use the TextInfo.ToTitleCase() method. In the title case, all words are capitalized.

Download  Run Code

 
To get the culture-independent invariant, you can use the CultureInfo.InvariantCulture property.

Download  Run Code

That’s all about converting the first letter of a string to uppercase in C#.