How to use Padding String operation in VB.NET

In this article I will describe a basic string operation that is padding.
  • 2352

The System.String and System.Text.StringBuilder are mostly used to construct custom strings which is displayed in user interface. Here I will discuss about a more method which also perform a string operation, the Padding String method.

Padding Strings

Padding is basically about to how you can insert characters or empty spaces into a string. We have two given below methods for padding the string ' first is used to right-alignment and second is used to left-alignment ' of the existing string with an specific spaces.

  1. String.PadLeft

  2. String.PadRight

String.PadLeft: this method is used to create a new string from the existing string which is right-aligned in this the last character is a specified number of spaces from the first index of the string. you can also used your custom character for the spacing.

EXAMPLE

    Module Module1
        Sub Main()
            Dim Line As String = "Hi manish"
            Console.WriteLine(Line.PadLeft(20, "_"c))
            Console.ReadLine()
        End Sub
    End
Module

In this example we use Line.PadLeft method which creates a new string with 20 spaces from the left side.

OUTPUT

9.gif
 

String.PadRight: this method is used to create  a new string from the existing string which is left-aligned in this the string is extended a specified number of spaces to the right of the string's first index. you can also used your custom character for the spacing.

EXAMPLE

    Module Module1
        Sub Main()
            Dim Line As String = "Hi manish"
            Console.WriteLine(Line.PadRight(20, "_"c))
            Console.ReadLine()
        End Sub
    End
Module

In this example we use Line.PadRight method which creates a new string with 20 spaces from the right side.

OUTPUT

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.