C# String PadRight() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, PadRight() adds characters to the right side of a string until it reaches a total width. The text stays on the left, so it is left-aligned. Spaces are used unless you pick another character.

It is the natural way to build simple text tables: pad each name to the same width, and the next column starts in the same place on every line. A new string is returned; the original stays as it was, and long text is never cut.

This is an instance member of System.String. Call it on a string variable, such as text.PadRight(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: PadRight() 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 name = "Sam";

string spaced = name.PadRight(6);        // "Sam   "
string starred = name.PadRight(6, '*');  // "Sam***"
CallWhat it does
text.PadRight(10)Left-align in 10 characters using spaces.
text.PadRight(10, '.')Fill with dots, like a menu or table of contents.
name.PadRight(12) + valueStart the next column at a fixed position.
text.PadRight(2)Already 2 or more characters? It comes back unchanged.

A negative width throws ArgumentOutOfRangeException. In string interpolation, an alignment like $"{name,-6}" pads on the right too, and {name,6} pads on the left. PadRight is handy when you also want a custom fill character.

Library declarations
C#
public string PadRight(int totalWidth);
public string PadRight(int totalWidth, char paddingChar);

Parameters

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

Return Value

Return ValueDescription
stringA new string of length totalWidth, or the original text when it is already that long.

C# Examples

Example 1 — Pad on the Right and Make It Visible

Pad a short name with spaces, then with stars, and add a bar so the end is easy to see.

C#
using System;

class Program {
    static void Main() {
        string name = "Sam";

        Console.WriteLine(name.PadRight(6) + "|");
        Console.WriteLine(name.PadRight(6, '*'));
        Console.WriteLine(name.Length);
    }
}

How It Works

  1. Three to add. Sam is 3 characters, and the target width is 6.
  2. Show the invisible. The | after the padded text marks where the string ends, so the three spaces become visible.
  3. A custom filler. With '*' the same three places are filled with stars.
  4. Still three. name.Length prints 3, proving that padding built a new string.

Example 2 — Print a Neat Price List

Pad each item name with dots to the same width, then print the price right after it.

C#
using System;

class Program {
    static void Main() {
        string[] items = { "Tea", "Coffee", "Juice" };
        string[] prices = { "2", "3", "4" };

        for (int i = 0; i < items.Length; i++) {
            Console.WriteLine(items[i].PadRight(8, '.') + prices[i]);
        }
    }
}

How It Works

  1. Two parallel arrays. items[i] and prices[i] belong together, so one index walks both.
  2. Same width for every name. Each name is padded to 8 characters: 5 dots after Tea, 2 after Coffee, 3 after Juice.
  3. The prices line up. Because every left part is exactly 8 characters, each price is printed in column 9.
  4. Pick the width from the data. The longest name has 6 characters, so 8 leaves room for at least two dots.

Example 3 — Build a Fixed-Width Record

Pad fields into a record line, and see what happens when one field is longer than its width.

C#
using System;

class Program {
    static void Main() {
        string code = "AB";
        string label = "XYZWVU";

        string rec = code.PadRight(5, '_') + "|" + label.PadRight(3) + "|";
        Console.WriteLine(rec);
        Console.WriteLine(rec.Length);
    }
}

How It Works

  1. Short field. AB grows to AB___, exactly 5 characters.
  2. Long field. XYZWVU has 6 characters but the width is 3. PadRight neither pads nor cuts it.
  3. The record grows. 5 + 1 + 6 + 1 makes 13 characters instead of the planned 10, so a strict file format would break.
  4. Cut first if you must. For strict widths, shorten long values with Substring(0, width) before padding.

Frequently Asked Questions

It adds spaces, or a character you choose, to the right of a string until it reaches the total width you pass. The text becomes left-aligned.
Pad each column value to a fixed width with PadRight (for text) or PadLeft (for numbers), then join the columns. Interpolation alignment such as $"{name,-10}" works too.
No. A string that is already as long as the width, or longer, is returned unchanged.
Spaces at the end of a line are invisible. Add a marker such as "|" after the value, or use a visible padding character like '.' while testing.
No. Strings are immutable, so PadRight returns a new string and the original keeps its length.

Next: Remove()

Delete characters from a position (to the end, or a set count) and get a new string.

Remove() 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.

11 people found this page helpful