TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

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.

Char
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf