C# | PadLeft() Method

31 Jan, 2019
In C#, PadLeft() is a string method. This method is used to right-aligns the characters in String by padding them with spaces or specified character on the left, for a specified total length. This method can be overloaded by passing different parameters to it.
String.PadLeft Method(Int32)
String.PadLeft Method(Int32, Char)
String.PadLeft Method(Int32)
This method is used to right-aligns the characters in this string by padding them with spaces on the left. The parameter “totalWidth” will specify the number of padding characters in the string and this method will return a new string.
Syntax:
public string PadLeft(int totalWidth)
Parameter: This method will accept one parameter “totalWidth” which specify the number of padding characters in string. The type of this parameter is System.Int32.
Return Value: This method will return the string which pads the right portion of the string. The return value type is System.String.
Exception: If totalWidth is less than zero then it will arise ArgumentOutOfRangeException.
Example: Program to demonstrate the public string PadLeft(int totalWidth) method. The string is right-aligned and padded on the left with whitespace as needed to create a length of totalWidth. However, If totalWidth is less than or equal to the length of the string, the method returns a new string that is identical to string. For example, If the current string is “Geeks” and total width is 7 then PadLeft method returns ” Geeks”.
// C# program to illustrate the// String.PadLeft(totalWidth) methodusing System;class Geeks {      // Main Method    public static void Main()    {        string s1 = "GeeksForGeeks";           Console.WriteLine("String : " + s1);           // totalwidth is less than string length        Console.WriteLine("Pad  2 :'{0}'", s1.PadLeft(2));           // totalwidth is equal to string length        Console.WriteLine("Pad 13 :'{0}'", s1.PadLeft(13));           // totalwidth is greater then string length        Console.WriteLine("Pad 20 :'{0}'", s1.PadLeft(20));    }}
Output:
String : GeeksForGeeks
Pad  2 :'GeeksForGeeks'
Pad 13 :'GeeksForGeeks'
Pad 20 :'       GeeksForGeeks'

String.PadLeft Method(Int32, Char)
This method is used to right-aligns the characters in this string by padding them with specified character on the left. The parameter “totalWidth” will specify the number of padding characters in string and “paddingChar” is the specified Character.
Syntax:
public string PadLeft(int totalWidth, char paddingChar)

Parameter: This method accept two parameter “totalWidth” and “paddingChar“. The parameter “totalWidth” will specify the number of padding characters in string and type of this parameter is System.Int32. The parameter “paddingChar” will specify the padding character and type of this parameter is System.Char.
Return Value: This method will return a new string which will be equivalent to current string, but right-aligned and padded on the left with the characters specified by “paddingChar” parameter. If totalWidth is less than the length of the string, the method returns the same string. If totalWidth is equal to the length of the string, the method returns a new string that is identical to the current String. The return value type is System.String.
Exception: If totalWidth is less than zero then it will arise ArgumentOutOfRangeException.
Example: Program to demonstrate the public string PadLeft(int totalWidth, char paddingChar) method. The string is right-aligned and padded on the left with as paddingChar characters as needed to create a length of totalWidth. However, If totalWidth is less than or equal to the length of this instance, the method returns a new string that is identical to this instance. For example, If the current string is “Geeks” and totalWidth is 7 and paddingChar is ‘*’ then PadLeft method returns “**Geeks”.
// C# program to illustrate the// String.PadLeft(int totalWidth, // char paddingChar) methodusing System;  class Geeks {      // Main Method    public static void Main()    {                  string s1 = "GeeksForGeeks";        char pad = '*';        Console.WriteLine("String : " + s1);          // totalwidth is less than string length        Console.WriteLine("Pad 2 :'{0}'", s1.PadLeft(2, pad));          // totalwidth is equal to string length        Console.WriteLine("Pad 13 :'{0}'", s1.PadLeft(13, pad));          // totalwidth is greater then string length        Console.WriteLine("Pad 20 :'{0}'", s1.PadLeft(20, pad));    }}
Output:
String : GeeksForGeeks
Pad 2 :'GeeksForGeeks'
Pad 13 :'GeeksForGeeks'
Pad 20 :'*******GeeksForGeeks'

Important Points About PadLeft() Method:
If the PadLeft method pads the current instance with whitespace characters, this method does not modify the value of the current instance. Instead, it returns a new string that is padded with leading white space so that its total length is totalWidth characters.
If totalWidth is less than the length of String, the method returns a reference to the existing instance.
References:
https://msdn.microsoft.com/en-us/library/system.string.padleft1
https://msdn.microsoft.com/en-us/library/system.string.padleft2
CSharp-method
CSharp-string
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.