TheDeveloperBlog.com

Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

C# char.IsLower Methods

This C# example program demonstrates the char.IsLower method. IsLower tests for lowercase chars.

Char.IsLower. The char.IsLower method tests for lowercase letters.

It returns false on all characters except the lowercase "a" through "z". And char.IsUpper meanwhile returns true only on "A" through "Z".

Example. First, this example shows eight calls to the char.IsLower and char.IsUpper methods on the char type. It then shows the output of two if-statements with these calls. This is how the char.IsLower and char.IsUpper static methods are used.

Note: They return a Boolean, which you can use to test in an if-statement. If a char is not lowercase, they return false.

C# program that uses char.IsLower

using System;

class Program
{
    static void Main()
    {
	char c1 = 'a'; // Lowercase letter
	char c2 = 'b'; // Lowercase letter
	char c3 = 'C'; // Uppercase letter
	char c4 = '3'; // Digit

	if (char.IsLower(c1) || // True
	    char.IsLower(c2) || // True
	    char.IsLower(c3) || // False
	    char.IsLower(c4))   // False *
	{
	    Console.WriteLine("A char is lowercase.");
	}

	if (char.IsUpper(c1) || // False
	    char.IsUpper(c2) || // False
	    char.IsUpper(c3) || // True
	    char.IsUpper(c4))   // False *
	{
	    Console.WriteLine("A char is uppercase.");
	}
    }
}

Output

A char is lowercase.
A char is uppercase.

The first four lines declare four characters, two of which are lowercase letters. Next the char.IsLower tests are chained in an "or" conditional. The two if-statements the four example chars for lowercase or uppercase letters.

Note: In the first if, the first char is lower so only that test will be executed. In the second if, the third char is upper.

And: For non-letter chars such as digits and punctuation, IsLower and IsUpper will return false.

Change case. In the .NET Framework, you can also change the case of the characters by using the char.ToLower and char.ToLower method. Those methods actually perform the conversion, while IsLower and IsUpper just do the testing.

char.ToLower

Summary. We used the char.IsLower and char.ToUpper methods. These methods are actually contained in the System namespace. But because char is actually an alias for System.Char, you do not need to include it.

And: We saw how these methods will return false on input that is not a character such as a digit.