find and solve || findandsolve.com
Please wait.....

C# String PadLeft()

In the C# string PadLeft() 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. returns a new string that right-aligns the characters in this instance by padding them on the left with a specified Unicode.This method can be overloaded by passing different parameters to it like as given below

  • 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.

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.
  • Exception:it will arise ArgumentOutOfRangeException If totalWidth is less than zero then .


PadLeft  Example

public class Program
{
 public static void Main()
 {
   string str = "FindAndSolve";
        Console.WriteLine("String : " + str);
        // totalwidth is less than string length
        Console.WriteLine("Pad 3 :'{0}'", str.PadLeft(3));
        // totalwidth is equal to string length         Console.WriteLine("Pad 11 :'{0}'", str.PadLeft(11));
        // totalwidth is greater then string length
        Console.WriteLine("Pad 15 :'{0}'", str.PadLeft(15));
 } }

Output

String : FindAndSolve

Pad 3 :'FindAndSolve'

Pad 11 :'FindAndSolve'

Pad 15 :' FindAndSolve'


String.PadLeft Method(Int32, Char)

PadLeft 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.

public string PadLeft(int totalWidth, char paddingChar)
  • Parameter: This method accept two parameter one is “totalWidth” and another char “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: it will arise ArgumentOutOfRangeException If totalWidth is less than zero then .


Example PadLeft Method(Int32, Char)

public class Program
{
 public static void Main()
 {
    string str = "FindAndSolve";
        char pad = '*';
        Console.WriteLine("String : " + str);
        // totalwidth is less than string length
        Console.WriteLine("Pad 3 :'{0}'", str.PadLeft(3, pad));
        // totalwidth is equal to string length
        Console.WriteLine("Pad 11 :'{0}'", str.PadLeft(11, pad));
        // totalwidth is greater then string length
        Console.WriteLine("Pad 15 :'{0}'", str.PadLeft(15, pad));
 }
}

Output

String : FindAndSolve

Pad 3 :'FindAndSolve'

Pad 11 :'FindAndSolve'

Pad 20 :'***FindAndSolve'

Important Points About PadLeft() Method:

  • Whitespace characters in the PadLeft method pads the current instance with, PadLeft 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  int totalWidth variable value is less than the length of String  then returns a reference to the existing instance.