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.
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.
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.
ToLower
to test if a String
is already lowercased.string
equals the result of ToLower
. This internally calls String.Equals
.ToLower
function. A CultureInfo
influences how non-ASCII characters are converted.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 Moduleabc123 Is Lower
ToUpper
exampleToUpper
converts all characters to uppercase characters. It causes a copy to be made of the VB.NET String
, which is returned.
ToUpper
and its behavior. This console program shows the result of ToUpper
on the input String
"abc123".ToUpper
Function.Module Module1 Sub Main() Dim value1 As String = "abc123" Dim upper1 As String = value1.ToUpper() Console.WriteLine(upper1) End Sub End ModuleABC123
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.
For-Each
loop and then the Char.IsLower
function.Char
is lowercase, the String
is not already uppercase. The code returns False early at that point.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 ModuleISUPPERCASE: True ISUPPERCASE: False ISUPPERCASE: True
ToTitleCase
To use the ToTitleCase()
function, we must access the TextInfo
class
. This is a bit more complex. In a title case string
, each word starts with a capital.
string
"can of worms" is uppercased with ToTitleCase
. But "Of" is typically not supposed to be uppercased for a title.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 ModuleINPUT: can of worms OUTPUT: Can Of Worms
ToUpper
function changes no characters except lowercase characters. Digits and uppercase letters (as well as punctuation and spaces) are left the same.
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.