C# Lowercase/Uppercase Articles

There are many different ways to lowercase and uppercase strings in the C# language, transforming "ABC" to "abc" and back again. However, some ways are more efficient and lead to faster code than others, and sometimes you don't need to change cases at all.

Introduction to lowercase/uppercase

For a short introduction, this program simply invokes the ToUpper and ToLower instance methods on the string instance. You can see that the lowercase characters are all converted into uppercase characters when ToUpper is called, and the reverse occurs when ToLower is called.

--- Program that uppercases/lowercases [C#] ---

using System;

class Program
{
    static void Main()
    {
        string value = "perls";

        // Convert to uppercase.
        value = value.ToUpper();
        Console.WriteLine(value);

        // Convert to lowercase.
        value = value.ToLower();
        Console.WriteLine(value);
    }
}

--- Output of the program ---

PERLS
perls

More details. The ToLower and ToUpper instance methods on the string type are the most commonly used character case conversion methods. These two articles describe the usage of these methods.

See ToLower String Method.

See ToUpper Method.

Uppercasing letters and words

Sometimes, you want to only uppercase the first character in a string, or the first character in each word in a string. These articles provide example code for accomplishing these tasks.

See Uppercase First Letter.

See Uppercase Words in String.

Optimizing case conversions

If you are interesting in optimizing your program, it is sometimes useful to improve the performance of character case conversions. We show how to use a lookup table for this purpose, and also how to scan strings to determine if any conversions are necessary in the first place.

See Char Lowercase Optimization.

See String IsUpper and IsLower Methods.

Invariant methods

The .NET Framework provides invariant case conversion methods. The term invariant refers to how the behavior of the method won't change based on the current culture of the machine.

See ToLowerInvariant and ToUpperInvariant Methods.

Lowercasing characters

Next, the .NET Framework has methods that let you uppercase and lowercase single characters. These articles reveal how you can use these methods to convert the letter 'a' into 'A' and back again.

See char.IsLower Methods.

See char.ToLower Methods.

Case-insensitivity

It is sometimes better to compare data using a case-insensitive method, rather than to normalize it all to lowercase or uppercase. This can result in improved performance in some programs. This article provides a tutorial for case-insensitive comparisons.

See Case-Insensitive string.Equals Method.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen