C# String.ToUpper() – Examples
String.ToUpper() method is used to get a copy of the given string converted to uppercase.
This method does not modify the original string, but returns a new string in which all characters in the given string are converted to uppercase.
In this tutorial, we will learn about the syntax of C# String.ToUpper() and C# String.ToUpper() methods, and learn how to use these method with the help of examples.
ToUpper()
String.ToUpper() returns a copy of this string converted to uppercase.
Syntax
The syntax of ToUpper() method is
String.ToUpper()
Return Value
This method returns String value.
Example 1 – ToUpper()
In this example, we will take a string with some upper-case and some lower-case alphabets, say "Hello World"
. To get uppercase of this string we will call ToUpper() method on this string. A string with all the upper case characters for the given string should be returned by ToUpper() method.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "Hello World";
String result = str.ToUpper();
Console.WriteLine($"Original String is : {str}");
Console.WriteLine($"Result of ToUpper() : {result}");
}
}
Output
Original String is : Hello World
Result of ToUpper() : HELLO WORLD
Example 2 – ToUpper() – All Lowercase Characters
In this example, we will take a string with all lowercase alphabets, say abcdef
. To get uppercase of this string we will call ToUpper() method on this string. A string with all the uppercase characters for the given string should be returned.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "abcdef";
String result = str.ToUpper();
Console.WriteLine($"Original String is : {str}");
Console.WriteLine($"Result of ToUpper() : {result}");
}
}
Output
Original String is : abcdef
Result of ToUpper() : ABCDEF
Example 3 – ToUpper() – Null String
In this example, we will take a null string and call ToUpper() method on this null string. ToUpper() should throw System.NullReferenceException.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = null;
String result = str.ToUpper();
Console.WriteLine($"Original String is : {str}");
Console.WriteLine($"Result of ToUpper() : {result}");
}
}
Output
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Example.Main(String[] args) in D:\workspace\csharp\HelloWorld\Program.cs:line 6
ToUpper(CultureInfo)
String.ToUpper() returns a copy of this string converted to uppercase, using the casing rules of the specified culture.
Syntax
The syntax of ToUpper(CultureInfo) method is
String.ToUpper(CultureInfo)
Return Value
This method returns String value.
Example 4 – ToUpper(culture)
In this example, we will take a Turkish character in the string. This character is lower case a
with double dot on top of it. We will convert this string into upper case, with culture information provided as argument to ToLower() method.
C# Program
using System;
using System.Globalization;
class Example {
static void Main(string[] args) {
String str = new String(new Char[] {'\u00E4'});
String result = str.ToUpper(new CultureInfo("tr-TR", false));
Console.WriteLine($"Original String is : {str}");
Console.WriteLine($"Result of ToUpper() : {result}");
}
}
Output
Original String is : ä
Result of ToUpper() : Ä
Conclusion
In this C# Tutorial, we have learnt the syntax of C# String.ToUpper() method, and also learnt how to use this method with the help of C# example programs.