C# String - ToUpper() Method
The C# String ToUpper() method converts all character in a string to upper case and is used to perform case-insensitive operations, normalize strings for comparison, or handle user input.
Syntax
Following are the syntax of the C# string ToUpper() method −
Default Syntax −
This syntax return copy of the current string converted to lower case.
public string ToUpper();
Parametrised Syntax −
This syntax returns a copy of this string converted to uppercase, using the casing rules of the specified culture.
public string ToUpper(System.Globalization.CultureInfo? culture);
Parameters
This method accepts the following parameters −
- culture: This is an object that supplies culture-specific wrapper rules. If culture is null, the current culture is used.
Return Value
This method returns a string that is in uppercase equivalent to the current string.
Example 1: Using ToUpper with Current Culture
Following is a basic example of the ToUpper() method to convert the string into uppercase with the current culture −
using System;
class Program {
public static void Main() {
string str = "Hello, tpians!";
string ToUpper = str.ToUpper();
Console.Write("Converted String: "+ ToUpper);
}
}
Output
Following is the output −
Converted String: HELLO, TPIANS!
Example 2: Using ToUpper(CultureInfo) with a specific culture
Let's look at another example. Here, we use the parametrised ToUpper() method to convert a string into uppercase with specific culture −
using System;
using System.Globalization;
class Program {
static void Main() {
// Turkish character ''
string str = "stanbul";
//use InvariantCulture
string lowerInvariant = str.ToUpper(CultureInfo.InvariantCulture);
string lowerTurkish = str.ToUpper(new CultureInfo("tr-TR"));
Console.WriteLine(lowerInvariant);
Console.WriteLine(lowerTurkish);
}
}
Output
Following is the output −
STANBUL STANBUL
Example 3: Case-Insensitive Comparisons
In this example, we convert the string into uppercase using the ToUpper() method. We then compare string −
using System;
class Program {
public static void Main() {
string userInput = "Admin";
if (userInput.ToUpper() == "ADMIN")
{
Console.WriteLine("Access granted!");
}
}
}
Output
Following is the output −
Access granted!