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 PadLeft and PadRight

Call the PadLeft and PadRight Functions on the String type to add whitespace.
PadLeft, PadRight. Padding changes the length of Strings. It adds extra characters to the right or left of the String data. PadLeft adds spaces to the left side. And PadRight adds to the right. Both also support other padding characters.Strings
This program creates an array filled with three Strings. It then loops over these Strings. Each String has up to five characters of padding added to the left. This new String returned by PadLeft is then passed to Console.WriteLine.Console

Info: PadLeft does not modify the String data. Instead it creates a modified String in new memory.

So: This means the String Dim you call it on—in this program "w"—is not modified in any way.

For Each, For
VB.NET program that uses PadLeft Module Module1 Sub Main() Dim words() As String = {"A", "Net", "Perls"} ' Loop over words. For Each w As String In words Console.WriteLine(w.PadLeft(5)) Next End Sub End Module Output A Net Perls
Example 2. The PadLeft Function is paired with the PadRight Function. In this example, we add complexity by using a Dictionary collection. For keys we use Strings. For values we use Integers.DictionaryInteger

Here: We loop over the Dictionary and apply padding to the Key. For the value we first convert the Integer to a String.

KeyValuePair
VB.NET program that pads keys and values Module Module1 Sub Main() ' Create Dictionary. Dim dict As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer) dict.Add("Cat", 5) dict.Add("Mouse", 1) dict.Add("Leopard", 9) dict.Add("Asp", 10) ' Loop over pairs. For Each pair As KeyValuePair(Of String, Integer) In dict ' Pad right of key. Console.Write(pair.Key.PadRight(10)) ' Pad left of value. Console.WriteLine(pair.Value.ToString().PadLeft(2)) Next End Sub End Module Output Cat 5 Mouse 1 Leopard 9 Asp 10
Example 3. PadLeft and PadRight are not restricted to only adding spaces. Instead they can add any Char value you like. In this program we use a period as the second argument to PadRight. And for PadLeft we pass a hash sign.

And: The output String contains padding with these special characters, not spaces.

VB.NET program that uses special characters Module Module1 Sub Main() ' Pad with . and # Console.Write("Sam".PadRight(10, "."c)) Console.WriteLine("28173".PadLeft(6, "#"c)) ' Next line. Console.Write("Mark".PadRight(10, "."c)) Console.WriteLine("137".PadLeft(6, "#"c)) End Sub End Module Output Sam.......#28173 Mark......137
Summary. Text-based display of data is common. It is an easy way to format tables and display them in console programs. I commonly use PadLeft and its friend PadRight in code that displays numeric data in console programs.
© 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