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.
- Declare
upperas astring. Assign itword.ToUpper(), which givesHELLO WORLD. - Declare
loweras astringbelow it. Assign itword.ToLower(), which giveshello 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,toLowerCaseandupper()do not exist on a C# string. The names areToUpperandToLower. - Forgetting the parentheses.
word.ToUpperwithout()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 useStringComparison.OrdinalIgnoreCase.
Key takeaways
ToUpper()andToLower()return a new string with the case changed.- The original string is not modified, and non-letters are left alone.
- Use the
Invariantversions orOrdinalIgnoreCasefor case-insensitive comparisons in code. - For one
char, usechar.ToUpperandchar.ToLower.