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.
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
.
Char.ToLower
on the 4 characters we declared. We see that only the uppercase Char
"C" was modified by this call.Char.ToUpper
on the same 4 characters. The lowercase characters were returned by this case.IsUpper
tells us that this is true.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 ModuleINITIAL: a b C 3 TOLOWER: a b c 3 TOUPPER: A B C 3 Char C is upper Char a is lower
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.