HomeSearch

C# char.IsLower and IsUpper

Call the char.IsLower method to determine if a char is lowercase or not.
Char.IsLower. This 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".Char
First, this example shows 8 calls to the char.IsLower and char.IsUpper methods on the char type. It then shows the output of 2 if-statements with these calls. This is how the char.IsLower and char.IsUpper static methods are used.

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

If

Here: The char.IsLower tests are chained in an "or" conditional. The if-statements test the 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.

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.
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.

© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls