VB.NET ToLower and ToUpper Examples

Use the ToLower and ToUpper Functions on strings to change the cases of letters.
ToLower, ToUpper. ToLower changes Strings that contain uppercase letters. We convert all the uppercase letters in a String to lowercase letters. With ToLower this is easy to do.Strings
Function notes. We can also use ToLower to determine if a String is already lowercased. With ToUpper we uppercase a string. With special logic, we can uppercase only the first letter.
An example. This program uses the ToLower function. In the first part, we lowercase the String "ABC123." Notice how the non-uppercase letters "123" are kept the same in the output.Function

Then: In the second part, we show how to use ToLower to test if a String is already lowercased.

Tip: To do this, we see if the original string equals the result of ToLower. This internally calls String.Equals.

Argument: It is possible to use an argument to the ToLower function. A CultureInfo influences how non-ASCII characters are converted.

VB.NET program that uses ToLower Module Module1 Sub Main() ' Convert string to lowercase. Dim value As String = "ABC123" value = value.ToLower() Console.WriteLine(value) ' See if a String is already lowercase. Dim cat As String = "cat" If cat = cat.ToLower() Then Console.WriteLine("Is Lower") End If End Sub End Module Output abc123 Is Lower
ToUpper example. ToUpper converts all characters to uppercase characters. It causes a copy to be made of the VB.NET String, which is returned.

Here: We look at ToUpper and its behavior. This console program shows the result of ToUpper on the input String "abc123".

Info: Notice how "abc" are the only characters that were changed. The non-lowercase letters are not changed.

Also: Characters that are already uppercase are not changed by the ToUpper Function.

VB.NET program that calls ToUpper on String Module Module1 Sub Main() Dim value1 As String = "abc123" Dim upper1 As String = value1.ToUpper() Console.WriteLine(upper1) End Sub End Module Output ABC123
IsUppercase. How can you determine if a String is already uppercase? We can use ToUpper and then compare the original String. If they are equal, the string was already uppercased.

However: This is not the most efficient way. A faster way uses a For-Each loop and then the Char.IsLower function.

For Each, For

And: If a Char is lowercase, the String is not already uppercase. The code returns False early at that point.

VB.NET program that tests for uppercase strings Module Module1 Function IsUppercase(ByRef value As String) As Boolean ' Loop over all characters in the string. ' ... Return false is one is lowercase. ' Otherwise, return true (all letters are uppercase). For Each letter As Char In value If Char.IsLower(letter) Then Return False End If Next Return True End Function Sub Main() Console.WriteLine("ISUPPERCASE: {0}", IsUppercase("BIRD")) Console.WriteLine("ISUPPERCASE: {0}", IsUppercase("Bird")) Console.WriteLine("ISUPPERCASE: {0}", IsUppercase("")) End Sub End Module Output ISUPPERCASE: True ISUPPERCASE: False ISUPPERCASE: True
ToTitleCase. To use the ToTitleCase function, we must access the TextInfo class. This is a bit more complex. A title case string Starts Each Word With a Capital.TextInfo

Warning: Here the string "can of worms" is uppercased with ToTitleCase. But "Of" is typically not supposed to be uppercased for a title.

VB.NET program that uses TextInfo, ToTitleCase Imports System.Globalization Module Module1 Sub Main() Dim value As String = "can of worms" ' Uppercase words with ToTitleCase. Dim info As TextInfo = CultureInfo.InvariantCulture.TextInfo Dim result As String = info.ToTitleCase(value) Console.WriteLine("INPUT: {0}", value) Console.WriteLine("OUTPUT: {0}", result) End Sub End Module Output INPUT: can of worms OUTPUT: Can Of Worms
A review. ToUpper function changes no characters except lowercase characters. Digits and uppercase letters (as well as punctuation and spaces) are left the same.
A summary. We looked at ToLower and ToUpper. These functions return a copy of the original string. With ToLower, all a string's uppercase letters are changed to lowercase letters.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to