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.
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.
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.
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.
Detail It is possible to use an argument to the 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 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.
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.
However This is not the most efficient way. A faster way uses a For-Each loop and then the Char.IsLower function.
And If a 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. A title case string Starts Each Word With a Capital.
Detail Here the 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
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 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.