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.
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
Parameter
Description
totalWidth
Required. The length the result should have. Must not be negative.
paddingChar
Optional. The character to add on the right. The default is a space.
Return Value
Return Value
Description
string
A 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);
}
}
Output
Sam |
Sam***
3
How It Works
Three to add.Sam is 3 characters, and the target width is 6.
Show the invisible. The | after the padded text marks where the string ends, so the three spaces become visible.
A custom filler. With '*' the same three places are filled with stars.
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]);
}
}
}
Output
Tea.....2
Coffee..3
Juice...4
How It Works
Two parallel arrays.items[i] and prices[i] belong together, so one index walks both.
Same width for every name. Each name is padded to 8 characters: 5 dots after Tea, 2 after Coffee, 3 after Juice.
The prices line up. Because every left part is exactly 8 characters, each price is printed in column 9.
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.
Short field.AB grows to AB___, exactly 5 characters.
Long field.XYZWVU has 6 characters but the width is 3. PadRight neither pads nor cuts it.
The record grows. 5 + 1 + 6 + 1 makes 13 characters instead of the planned 10, so a strict file format would break.
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.