How to Capitalize the First Letter of a String in C#

Published: Monday, May 9, 2022

Greetings, friends! Here is a simple approach for capitalizing the first letter of a string using C#.

csharp
Copied! ⭐️
using System;

public class Program
{
    public static void Main()
    {
        string str = "greetings!";
        Console.WriteLine(char.ToUpper(str[0]) + str.Substring(1));
        // OUTPUT: Greetings!
    }
}

You can try out this code by copying and pasting it to .NET Fiddle.

In the code above, we can grab the first letter of a string by using str[0]. This will return a Char structure, which represents a single character. We can then use char.ToUpper to uppercase the first letter.

The String.Substring method retrieves part of a string, starting at a specified index. By passing in the value, 1, to this method, we are extracting the entire string except the first character.

Finally, we can use string concatenation to create the capitalized string!

Resources