.PadLeft()

teja_99's avatar
Published Nov 16, 2024
Contribute to Docs

In C#, the .PadLeft() method adds padding to the start of a string until it reaches the specified total length. If a padding character is not specified, it defaults to spaces. This method returns a new string, leaving the original string unchanged.

Syntax

string.PadLeft(int totalWidth);

Or, alternatively:

string.PadLeft(int totalWidth, char paddingChar);
  • totalWidth: The desired total length of the string after padding.
  • paddingChar (Optional): The character used for padding. If not specified, a space is used by default.

Example

The following example illustrates the usage of .PadLeft() in various scenarios:

using System;
class Program
{
static void Main()
{
string str = "Codecademy"; // Length: 10
Console.WriteLine($"String: '{str}'");
// Case 1: Padding with default space character
string paddingDefault = str.PadLeft(15);
Console.WriteLine($"Case 1: '{paddingDefault}'");
// Case 2: Padding with a specific character
char paddingChar = '.';
string paddingWithChar = str.PadLeft(15, paddingChar);
Console.WriteLine($"Case 2: '{paddingWithChar}'");
// Case 3: Total width is less than string length
string noPadding = str.PadLeft(1);
Console.WriteLine($"Case 3: '{noPadding}'");
}
}

The above example gives the following output:

String: 'Codecademy'
Case 1: ' Codecademy'
Case 2: '.....Codecademy'
Case 3: 'Codecademy'

Codebyte Example

The following runnable example demonstrates different use cases for .PadLeft(), including padding with spaces by default and padding with a specified character:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C# on Codecademy