In C#, PadLeft() adds characters to the left side of a string until it is a certain total width. By default it adds spaces; you can choose another character, such as '0'. The text ends up right-aligned.
The number you pass is the final length, not how many characters to add. "42".PadLeft(5) adds three spaces to reach 5. If the string is already that long or longer, nothing is added and nothing is cut. A new string is returned; the original is unchanged.
This is an instance member of System.String. Call it on a string variable, such as text.PadLeft(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: PadLeft() never changes the original text. It gives back a new string (or the same one when there is nothing to change), so keep the result in a variable.
Syntax
Pass the total width, and optionally the padding character.
If text is already 2 or more characters, it is returned unchanged.
A negative totalWidth throws ArgumentOutOfRangeException. For numbers, a format string can do the same job: 42.ToString("D5") also gives 00042. PadLeft works on any text, not only numbers.
Library declarations
C#
public string PadLeft(int totalWidth);
public string PadLeft(int totalWidth, char paddingChar);
Parameters
Parameter
Description
totalWidth
Required. The length the result should have. Must not be negative.
paddingChar
Optional. The character to add. The default is a space.
Return Value
Return Value
Description
string
A new string of length totalWidth, or the original text if it was already that long.
C# Examples
Example 1 — Pad with Spaces and with Zeros
Pad the same number two ways. Brackets show where the spaces went.
C#
using System;
class Program {
static void Main() {
string n = "42";
Console.WriteLine("[" + n.PadLeft(5) + "]");
Console.WriteLine(n.PadLeft(5, '0'));
Console.WriteLine(n);
}
}
Output
[ 42]
00042
42
How It Works
Width, not count."42" has 2 characters, so reaching width 5 needs 3 more.
Spaces by default. The brackets show the three spaces sitting to the left of the digits.
Any filler. Passing '0' turns the same call into leading zeros: 00042.
Untouched source. The last line shows n is still the two-character 42.
Example 2 — Right-Align a Column of Numbers
Line up numbers of different lengths so their last digits sit in the same column. Dots make the padding easy to see.
C#
using System;
class Program {
static void Main() {
string[] nums = { "7", "42", "315" };
foreach (string n in nums) {
Console.WriteLine(n.PadLeft(5, '.'));
}
}
}
Output
....7
...42
..315
How It Works
Different lengths. The values have 1, 2, and 3 characters.
One width for all. Each is padded to 5, so shorter values get more dots: 4, then 3, then 2.
Aligned on the right. The last digits line up in one column, which makes numbers easy to compare by eye.
Use spaces for real reports. Swap the '.' for a space, or leave out the second argument, for a clean console table.
Example 3 — Build Invoice Codes, and Leave Long Text Alone
Use leading zeros to build a fixed-width invoice code, and see that a value that is already wider is not cut.
C#
using System;
class Program {
static void Main() {
int id = 57;
string invoice = "INV-" + id.ToString().PadLeft(4, '0');
Console.WriteLine(invoice);
Console.WriteLine("123456".PadLeft(3));
}
}
Output
INV-0057
123456
How It Works
Number to text.PadLeft is a string method, so id.ToString() turns 57 into "57" first.
Two zeros added. Width 4 minus 2 digits leaves 2 places, so the code becomes 0057 and every invoice number has the same length.
Too wide already."123456" has 6 characters and the width is 3. Nothing is added and nothing is removed.
Plan for overflow. Invoice 12345 would print as INV-12345, one character wider. Pick a width that fits your largest number.
Frequently Asked Questions
It adds spaces, or a character you choose, to the left of a string until it reaches the total width you give. The result is right-aligned text.
Use value.ToString().PadLeft(width, '0'), for example "7".PadLeft(3, '0') gives "007". For integers, value.ToString("D3") does the same.
No. If the string is already as long as totalWidth or longer, PadLeft returns it unchanged.
No, it is the final total length. "ab".PadLeft(5) adds 3 spaces.
PadLeft adds characters on the left, which right-aligns text. PadRight adds them on the right, which left-aligns text.