ZetCode

C# ToUpper

last modified October 21, 2020

C# ToUpper tutorial shows how to transform letters to upper case in C# language. C# tutorial is a comprehensive tutorial on C# language.

The string.ToUpper method returns a copy of a string converted to uppercase. The overloaded ToUpper(CultureInfo) returns a copy of a string converted to uppercase, using the casing rules of the specified culture.

C# ToUpper example

In the following example, we transform the specified strings to uppercase.

Program.cs
using System;


var w1 = "stormy weather";
var w2 = "l'écrivain français";
var w3 = "ясный сокол";
var w4 = "červená hviezda";


Console.WriteLine(w1.ToUpper());
Console.WriteLine(w2.ToUpper());
Console.WriteLine(w3.ToUpper());
Console.WriteLine(w4.ToUpper());

We have fours strings inf four different languages. We change the letters to uppercase with ToUpper.

$ dotnet run
STORMY WEATHER
L'ÉCRIVAIN FRANÇAIS
ЯСНЫЙ СОКОЛ
ČERVENÁ HVIEZDA

All the letters were transformed to uppercase.

C# ToTitleCase

The TextInfo.ToTitleCase converts the specified string to title case.

Program.cs
using System;
using System.Globalization;


var w1 = "stormy weather";
var w2 = "l'écrivain français";
var w3 = "ясный сокол";
var w4 = "červená hviezda";

Console.WriteLine(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(w1));
Console.WriteLine(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(w2));
Console.WriteLine(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(w3));
Console.WriteLine(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(w4));

We transform the four strings with TextInfo.ToTitleCase.

$ dotnet run
Stormy Weather
L'écrivain Français
Ясный Сокол
Červená Hviezda

The TextInfo.ToTitleCase changed to uppercase the first letters of every word.

Often, we only want to change the first letter of the first word of a string. For this, we need to create our own function.

Program.cs
using System;
using System.Globalization;


var w1 = "stormy weather";
var w2 = "l'écrivain français";
var w3 = "ясный сокол";
var w4 = "červená hviezda";


Console.WriteLine(TitleCase(w1));
Console.WriteLine(TitleCase(w2));
Console.WriteLine(TitleCase(w3));
Console.WriteLine(TitleCase(w4));


string TitleCase(string s)
{
    if (string.IsNullOrEmpty(s))
    {
        return string.Empty;
    }

    char[] a = s.ToCharArray();
    a[0] = char.ToUpper(a[0]);
    return new string(a);
}

The TitleCase function uppercases only the first word of a string.

string TitleCase(string s)
{
    if (string.IsNullOrEmpty(s))
    {
        return string.Empty;
    }

    char[] a = s.ToCharArray();
    a[0] = char.ToUpper(a[0]);
    return new string(a);
}

In the TitleCase function, we transform the string into an array of characters with ToCharArray. Then we change the first character in the array to uppercase and return a new string created from the modified array.

$ dotnet run
Stormy weather
L'écrivain français
Ясный сокол
Červená hviezda

This is the output.

In this tutorial we have transformed strings to uppercase in C# language.

Read C# tutorial or list all C# tutorials.