C# String PadLeft() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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.

C#
string n = "42";

string spaced = n.PadLeft(5);         // "   42"
string zeros  = n.PadLeft(5, '0');    // "00042"
CallWhat it does
text.PadLeft(8)Right-align in 8 characters, using spaces.
text.PadLeft(4, '0')Add leading zeros up to 4 characters.
id.ToString().PadLeft(6, '0')Format a number as a fixed-width code.
text.PadLeft(2)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

ParameterDescription
totalWidthRequired. The length the result should have. Must not be negative.
paddingCharOptional. The character to add. The default is a space.

Return Value

Return ValueDescription
stringA 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);
    }
}

How It Works

  1. Width, not count. "42" has 2 characters, so reaching width 5 needs 3 more.
  2. Spaces by default. The brackets show the three spaces sitting to the left of the digits.
  3. Any filler. Passing '0' turns the same call into leading zeros: 00042.
  4. 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, '.'));
        }
    }
}

How It Works

  1. Different lengths. The values have 1, 2, and 3 characters.
  2. One width for all. Each is padded to 5, so shorter values get more dots: 4, then 3, then 2.
  3. Aligned on the right. The last digits line up in one column, which makes numbers easy to compare by eye.
  4. 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));
    }
}

How It Works

  1. Number to text. PadLeft is a string method, so id.ToString() turns 57 into "57" first.
  2. Two zeros added. Width 4 minus 2 digits leaves 2 places, so the code becomes 0057 and every invoice number has the same length.
  3. Too wide already. "123456" has 6 characters and the width is 3. Nothing is added and nothing is removed.
  4. 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.

Next: PadRight()

Add characters on the right until the string reaches a total width (left-align).

PadRight() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful