C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
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.
CharVB.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
However: This is not the most efficient way. A faster way uses a For-Each loop and then the Char.IsLower function.
For Each, ForAnd: 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
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