Summary: in this tutorial, you will learn how to use the C# String ToUpper()
method to convert a string to uppercase.
Introduction to the C# ToUpper() method
The String ToUpper()
method returns a copy of the current string converted to uppercase. Here’s the syntax of the ToUpper()
method:
public string ToUpper();
Code language: PHP (php)
The following example uses the ToUpper()
method to convert the message "Hello, World!"
to uppercase:
using static System.Console;
var message = "Hello, World!";
var upper = message.ToUpper();
WriteLine(upper);
Code language: C# (cs)
Output:
HELLO, WORLD!
Code language: plaintext (plaintext)
By default, the ToUpper()
method takes the case rule of the current culture into the conversion. But if you want to convert a string to uppercase using a specific culture, you can use the following overload of the ToUpper()
method:
public string ToLower (
System.Globalization.CultureInfo? culture
);
Code language: C# (cs)
In this syntax, the culture
is an instance of CultureInfo
that specifies a culture-specific casing rule.
For example, the following program illustrates how to use different CultureInfo
objects can lead to different uppercase conversions based on specific language rules:
using System.Globalization;
using static System.Console;
var s = "istanbul İzmir Ankara";
// Using the default (English) culture
var englishUppercase = s.ToUpper(CultureInfo.InvariantCulture);
WriteLine("Default (English) uppercase: " + englishUppercase);
// Using the Turkish culture
var turkishCulture = new CultureInfo("tr-TR");
var turkishUppercase = s.ToUpper(turkishCulture);
WriteLine("Turkish uppercase: " + turkishUppercase);
Code language: C# (cs)
In this example, we have a string that contains Turkish characters, such as "İ"
(capital İ
with a dot) and "İ"
(small i
with a dot) which are specific to the Turkish alphabet.
When we use the ToUpper()
to convert a string to uppercase using the default culture (English), the method uses the English rules for uppercase conversion, and we get "ISTANBUL İZMIR ANKARA"
.
However, when we use the ToUpper()
method with the Turkish culture ("tr-TR"
), the method uses Turkish-specific uppercase rules for conversion.
In Turkish, the uppercase equivalent of "i"
is "İ"
without a dot, and the uppercase equivalent of "İ"
is "I"
(capital I without a dot). As a result, the Turkish uppercase string becomes "İSTANBUL İZMİR ANKARA"
.
C# String ToUpperInvariant()
The String ToUpperInvariant()
returns an uppercase version of a string using the casing rules of the invariant culture.
Here’s the syntax of the ToUpperInvariant()
method:
public string ToUpperInvariant();
Code language: PHP (php)
The ToUpperInvariant()
method is equivalent to the ToUpper()
method with the CultureInfo.InvariantCulture
argument:
ToUpper(CultureInfo.InvariantCulture)
Code language: CSS (css)
One practical example of using the ToUpperInvariant()
method is when you need to perform a case-sensitive string comparison.
Using ToUpperInvariant()
ensures consistent behavior and avoids potential issues with culture-specific rules.
Summary
- Use the C# String
ToUpper()
method to convert a string to uppercase.