Open In App

C# | PadLeft() Method

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)



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)

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) method
using 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)

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) method
using 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:

References:


Article Tags :
C#