PadLeft and PadRight in C# and VB.Net

We can use PadLeft and PadRight methods for inserting characters with the given text in C#. Means, You can use Padleft and PadRight methods to add characters to the beginning and end of your text.

The padding character can be spaces or a specified character, and consequently appears to be either right-aligned or left-aligned. Suppose if you always need to show the account number in 10 characters and when account number doesn’t contain 10 characters then it should be left padded with ‘0’, for example, ’10N9843′ should be show as ‘00010N9843’. In this, we will use ‘0’ as padding character.

PadLeft

To pad a string on the left side, use these methods:

Public string padLeft(int len)
Public string padLeft(int len,char ch)

Where ‘len; is the length of characters in the resulting string so ‘len’ will be sum of the length of original characters and any additional padding characters. And ‘ch’ is the padding character.

PadRight

To pad a string on the right side, use these methods:

Public string padRight(int len)
Public string padRight(int len,char ch)

Same as above ‘len’ is the length of characters in the resulting string so ‘len’ will be sum of the length of original characters and any additional padding characters. The given total length (value of ‘len’) should be greater than zero otherwise ‘ArgumentOutOfRangeException’ exception will be thrown. And the ‘ch’ is the padding character.

Example

The following example demonstrates the ‘PadLeft’ and ‘padRight’ methods.

C#

using System;
 
namespace DotNetCoreExample_1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string str = "10N9843";
            string padLeftStr1 = str.PadLeft(10);
            Console.WriteLine(padLeftStr1);
            // value of padLeftStr1  is  '  10N9843'
 
           string padLeftStr2 = str.PadLeft(10, '0');
           Console.WriteLine(padLeftStr2);
            // value of padLeftStr2  is  '00010N9843'
 
            string padLeftStr3 = str.PadLeft(10, '*');
           Console.WriteLine(padLeftStr3);
            // value of padLeftStr3  is  '***10N9843'
 
            string padRightStr4 = str.PadRight(10);
             Console.WriteLine(padRightStr4);
            // value of padLeftStr4  is  '10N9843  '
 
            string padRightStr5 = str.PadRight(10, '*');
            Console.WriteLine(padRightStr5);
            // value of padLeftStr5  is  '10N9843***'
        }
    }
}

VB.Net

Imports System
 
Namespace DotNetCoreExample_1
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Console.WriteLine("Hello World!")
            Dim str As String = "10N9843"
            Dim padLeftStr1 As String = str.PadLeft(10)
            Console.WriteLine(padLeftStr1)
            Dim padLeftStr2 As String = str.PadLeft(10, "0"c)
            Console.WriteLine(padLeftStr2)
            Dim padLeftStr3 As String = str.PadLeft(10, "*"c)
            Console.WriteLine(padLeftStr3)
            Dim padRightStr4 As String = str.PadRight(10)
            Console.WriteLine(padRightStr4)
            Dim padRightStr5 As String = str.PadRight(10, "*"c)
            Console.WriteLine(padRightStr5)
        End Sub
    End Class
End Namespace

str.PadLeft(10) will insert blank space characters to the start of the string str. The total number of characters will be 10. So, PadLeft(10) will add 3 blank spaces.

str.PadLeft(10, ‘0’) will pad with the ‘0’ character, instead of the default blank spaces.

similarly str.PadRight(10) will insert blank space characters to the end of the string str.

Author: Hirendra Sisodiya

Programmer at PSPL. Can be found on Google Plus.