Open In App

C# String PadLeft() Method

Last Updated : 23 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The PadLeft() method in C# is a method of the String class. This method is very useful when we want to right-align the string by adding an extra space or padding on the left or the start of the string. It is used to format the text and modify its appearance.

This method can be overloaded by passing different parameters to it.

  • String.PadLeft Method(Int32)
  • String.PadLeft Method(Int32, Char)

Overloads of PadLeft() Method

1. String.PadLeft Method(Int32)

This method is used to right-align the characters in this string by padding them with spaces on the left. We can pass in the parameter that is the number of spaces we need to add to the left of a string.

Syntax:

public string PadLeft(int totalWidth)

  • Parameter: This method will accept one parameter, “totalWidth” which specifies the number of padding characters in the string. The type of this parameter is System.Int32.
  • Return Value: This method will return the string that pads the left portion of the string. The return value type is System.String.
  • Exception: If totalWidth is less than or equal to zero, then it will raise ArgumentOutOfRangeException.

Example: Using the PadLeft(totalWidth) method to add extra space on the left of a string.

C#
// C# program to illustrate the 
// String.PadLeft(totalWidth) method 
using System;

class Geeks
 { 
	public static void Main() 
	{ 
		string s = "GeeksForGeeks"; 

		Console.WriteLine("String : " + s); 

		// totalwidth is less than string length 
		Console.WriteLine("Padding 2 :'{0}'", s.PadLeft(2)); 

		// totalwidth is equal to string length 
		Console.WriteLine("Padding 13 :'{0}'", s.PadLeft(13)); 

		// totalwidth is greater then string length 
		Console.WriteLine("Padding 20 :'{0}'", s.PadLeft(20)); 
	} 
} 

Output
String : GeeksForGeeks
Padding 2 :'GeeksForGeeks'
Padding 13 :'GeeksForGeeks'
Padding 20 :'       GeeksForGeeks'

Explanation: In the above example, if the current string is 'GeeksForGeeks' and the total width is 7, the PadLeft() method will return the original string 'GeeksForGeeks' without any padding, as the totalWidth is less than or equal to the string length. If the padding is greater than the string length it will add the padding as shown in the output.


2. String.PadLeft Method(Int32, Char)

This method is used to right-align the characters in a string by padding them with a specified character on the left. The parameter “totalWidth” will specify the number of padding characters in the string.

Syntax:

public string PadLeft(int totalWidth, char paddingChar)

Parameters:

  • totalWidth: It will specify the number of padding characters in the string and the type of this parameter is System.Int32.
  • paddingChar: It will specify the padding character and the type of this parameter is System.Char.

Return Type: Return the string with the specified width if it is greater than the length of the string otherwise, return the identical string.

Exception: ArgumentOutOfRangeException: If totalWidth is less than zero.


Example 1: Using the PadLeft(int totalWidth, char paddingChar) to add the padding on the left with the specified character.

C#
// C# program to illustrate the 
// String.PadLeft(int totalWidth, 
// char paddingChar) method 
using System; 

class Geeks 
{
	public static void Main() 
	{		
		string s = "GeeksForGeeks"; 
		char pad = '*'; 
		Console.WriteLine("String: " + s); 

		// totalwidth is less than string length 
		Console.WriteLine("Pad 2:'{0}'", s.PadLeft(2, pad)); 

		// totalwidth is equal to string length 
		Console.WriteLine("Pad 13:'{0}'", s.PadLeft(13, pad)); 

		// totalwidth is greater then string length 
		Console.WriteLine("Pad 20:'{0}'", s.PadLeft(20, pad)); 
	} 
} 

Output
String: GeeksForGeeks
Pad 2:'GeeksForGeeks'
Pad 13:'GeeksForGeeks'
Pad 20:'*******GeeksForGeeks'

Explanation: In the above example, we use the PadLeft() method with the specified character and it adds the character to the left of the string if the totalWidth is greater than the length of the string otherwise it returns the identical string.

Real-World Example: Using the PadLeft() Method to Align the Column of Numbers

C#
// C# Program to allign the column of numbers
// by adding the extra space in the left 
// using PadLeft() method
using System;

public class Geeks 
{

    static public void Main()
    {
        // Array of numbers
        string[] arr = { "3", "35", "357", "3570" };
        
        
        foreach (var i in arr)
        {  
            // Adding extra space in left
            Console.WriteLine(i.PadLeft(5));
        }
    }
}

Output
    3
   35
  357
 3570

Explanation: In the above example, we use the PadLeft() method to which add extra space in the left side which makes it visually looking good. This is the real life use case of PadLeft() method mostly used to align the text to make look better.

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 the string, the method returns a new identical string.

Next Article
Practice Tags :

Similar Reads