Start learning free

LESSON 64 OF 347 · STRINGS I

Uppercase and Lowercase in C#: ToUpper and ToLower

Lesson Read the full explanation

In Java, String objects have built-in methods to change their case:

s.ToUpper()   // converts all characters to uppercase
s.ToLower()   // converts all characters to lowercase

Here are some examples:

string word = "Hello World";
string upper = word.ToUpper();  // "HELLO WORLD"
string lower = word.ToLower();  // "hello world"

string name = "ALICE";
Console.WriteLine(name.ToLower());  // "alice"

These methods return a new String — the original is not modified.


Assignment
Follow the Coding Tutorial and convert word to both uppercase and lowercase!


Hint
Use word.toUpperCase() and word.toLowerCase().

STEP 1 OF 2
    Ctrl + Enter to check

    C# · STRINGS I

    String Upper Lower in C#, explained

    Changing case

    Two string methods change the case of letters: ToUpper() converts every letter to uppercase and ToLower() converts every letter to lowercase. Both return a new string.

    string word = "Hello World";
    
    string upper = word.ToUpper();
    string lower = word.ToLower();
    
    Console.WriteLine(upper);  // HELLO WORLD
    Console.WriteLine(lower);  // hello world
    Console.WriteLine(word);   // Hello World
    

    The original is unchanged, because strings in C# are immutable. Calling word.ToUpper(); on a line by itself throws the result away, so assign it to a variable, or back to word if you want the change to stick. Characters that are not letters, such as digits, spaces and punctuation, stay exactly as they were.

    Comparing without caring about case

    The most common use of case conversion is a comparison that ignores case. In C#, == on strings compares their contents, and it is case-sensitive:

    string a = "Hello";
    string b = "hello";
    
    Console.WriteLine(a == b);                      // False
    Console.WriteLine(a.ToLower() == b.ToLower());  // True
    

    Converting both sides to the same case works. C# also has a method made for this, which doesn't create the two extra strings:

    Console.WriteLine(string.Equals(a, b, StringComparison.OrdinalIgnoreCase));  // True
    

    Culture and the invariant versions

    By default, ToUpper() and ToLower() follow the rules of the computer's current culture. In some languages, such as Turkish, the rules for the letter i differ from English. When the text is a code, a key or a file name rather than something shown to a user, use ToUpperInvariant() and ToLowerInvariant(), which behave the same everywhere:

    Console.WriteLine("Hello".ToUpperInvariant());  // HELLO
    

    For the plain English text in this lesson, both forms give identical results.

    Single characters

    A char is not a string, so it has no ToUpper method of its own. Use the static methods on char instead:

    char c = 'a';
    
    Console.WriteLine(char.ToUpper(c));  // A
    

    This helps when you capitalise only the first letter of a word: take it out with word[0], convert it and join it to word.Substring(1).

    Capitalising a word

    Putting the pieces together, this turns "hello" into "Hello":

    string word = "hello";
    
    string capitalised = char.ToUpper(word[0]) + word.Substring(1);
    Console.WriteLine(capitalised);  // Hello
    

    The expression adds a char and a string, and C# turns that into a string. For an empty word, word[0] would throw, so check the length first when the word may be empty.

    Normalising keys

    A related job is making user text match stored text. A lookup table keyed by lowercase names only works if the lookup text is lowercase too:

    var stock = new Dictionary<string, int>();
    stock["apple"] = 3;
    
    string input = " Apple ";
    string key = input.Trim().ToLower();
    
    Console.WriteLine(stock[key]);  // 3
    

    Chaining works because every call returns a string. Here Trim removes the spaces and ToLower fixes the case. A dictionary can also be created with StringComparer.OrdinalIgnoreCase so that it ignores case on its own.

    What is left alone

    Only letters have a case. Digits, spaces and punctuation pass through unchanged, so "Hello, World 123".ToUpper() gives HELLO, WORLD 123. Letters from other alphabets are converted by the same rules when they have an uppercase or lowercase form, so "ΑΒΓ".ToLower() gives αβγ. The conversion works one character at a time, so the new string has the same length as the old one. A letter such as the German ß has no single-character uppercase form and stays as it is.

    The exercise, step by step

    The lesson gives you word set to "Hello World" and asks for two new variables.

    1. Declare upper as a string. Assign it word.ToUpper(), which gives HELLO WORLD.
    2. Declare lower as a string below it. Assign it word.ToLower(), which gives hello world.
            string word = "Hello World";
    
            string upper = word.ToUpper();
            string lower = word.ToLower();
    

    Both calls start from word, so neither affects the other, and word keeps its original mixed case.

    Common mistakes

    • Using names from other languages. toUpperCase, toLowerCase and upper() do not exist on a C# string. The names are ToUpper and ToLower.
    • Forgetting the parentheses. word.ToUpper without () refers to the method itself and doesn't compile as a string.
    • Ignoring the result. The original string never changes.
    • Comparing with == and expecting case to be ignored. Convert both sides first, or use StringComparison.OrdinalIgnoreCase.

    Key takeaways

    • ToUpper() and ToLower() return a new string with the case changed.
    • The original string is not modified, and non-letters are left alone.
    • Use the Invariant versions or OrdinalIgnoreCase for case-insensitive comparisons in code.
    • For one char, use char.ToUpper and char.ToLower.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free