Char.ToLower and ToUpper
This page was last reviewed on Jun 18, 2024.
Dot Net Perls
Char.ToLower, ToUpper. In VB.NET the Char class contains many helpful character-testing and manipulation functions. We can convert characters to lowercase and uppercase.
In addition to conversion, we can test characters with IsLower and IsUpper. This allows to develop complex imperative logical methods to test characters and parts of strings.
Example. This program is written in the VB.NET programming language and it modifies and tests characters. It uses ToLower, ToUpper, and then IsLower and IsUpper.
Part 1 We call Char.ToLower on the 4 characters we declared. We see that only the uppercase Char "C" was modified by this call.
Part 2 We call Char.ToUpper on the same 4 characters. The lowercase characters were returned by this case.
Part 3 We try to determine if the value "C" is an uppercase character, and IsUpper tells us that this is true.
Part 4 Meanwhile, IsLower returns True on the character value "a", meaning this is a lowercase character.
Module Module1 Sub Main() Dim c1 = "a"c Dim c2 = "b"c Dim c3 = "C"c Dim c4 = "3"c Console.WriteLine($"INITIAL: {c1} {c2} {c3} {c4}") ' Part 1: use Char.ToLower. Dim lower1 = Char.ToLower(c1) Dim lower2 = Char.ToLower(c2) Dim lower3 = Char.ToLower(c3) Dim lower4 = Char.ToLower(c4) Console.WriteLine($"TOLOWER: {lower1} {lower2} {lower3} {lower4}") ' Part 2: use Char.ToUpper. Dim upper1 = Char.ToUpper(c1) Dim upper2 = Char.ToUpper(c2) Dim upper3 = Char.ToUpper(c3) Dim upper4 = Char.ToUpper(c4) Console.WriteLine($"TOUPPER: {upper1} {upper2} {upper3} {upper4}") ' Part 3: use Char.IsUpper. If Char.IsUpper(c3) Console.WriteLine($"Char {c3} is upper") End If ' Part 4: use Char.IsLower. If Char.IsLower(c1) Console.WriteLine($"Char {c1} is lower") End If End Sub End Module
INITIAL: a b C 3 TOLOWER: a b c 3 TOUPPER: A B C 3 Char C is upper Char a is lower
Summary. It is better to use Char.ToLower and ToUpper to transform character cases instead of writing custom code, as the logic is already tested. IsLower and IsUpper are also useful tools.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 18, 2024 (new).
© 2007-2024 Sam Allen.