C# PadRight

String type

PadRight adds spaces to the right of strings. It makes text easier to read or store in databases. Padding a string adds whitespace or other characters to the beginning or end. PadRight supports any character for padding, not just a space.

Output of PadRight method
    The left column is padded to 10 characters wide.

cat       feline
poodle    canine

Example 1

Here we look at an example of using the PadRight method to right align strings. I show several examples and usages for the Pad methods in the C# language. First, here is the code that would generate the output in the problem statement.

Program that uses PadRight [C#]

using System;

class Program
{
    static void Main()
    {
	string s = "cat".PadRight(10);
	string s2 = "poodle".PadRight(10);

	Console.Write(s);
	Console.WriteLine("feline");
	Console.Write(s2);
	Console.WriteLine("canine");
    }
}

Output
    Shown at top of article.

On both strings, the PadRight method is called with the number 10 as the parameter. This expands, or pads, the width of the string to 10 characters. Next in the example, we output the strings to the Console. Normally, padding strings is most useful for Console apps, debug output, or certain text formats.

Two-column layout

Here we look at another example of using the PadRight method. This next example is similar to the first one, but it uses a loop to build up a columnar, preformatted layout. It shows how you can call PadRight on string literals and also for strings in an array. The output of the example will be a simple table with two columns, very similar to the previous examples.

Program that uses PadRight [C#]

using System;

class Program
{
    static void Main()
    {
	string[] a = new string[]
	{
	    "cat",
	    "poodle",
	    "lizard",
	    "frog",
	    "photon"
	};

	// Output the header.
	Console.Write("Key".PadRight(15));
	Console.WriteLine("Value");

	// Output array.
	foreach (string s in a)
	{
	    Console.Write(s.PadRight(15));
	    Console.WriteLine("noun");
	}
    }
}

Output

Key            Value
cat            noun
poodle         noun
lizard         noun
frog           noun
photon         noun

Right-align numbers with PadLeft

In many applications, numeric values need to be aligned to the right. This is how Excel aligns numbers, for example. We see that the numbers on the right are aligned to the right edge, like Excel does. Here's the code that outputs the text above. Note how PadLeft is called on the header string too.

Program that uses PadRight and PadLeft [C#]

using System;

class Program
{
    static void Main()
    {
	int[] a = new int[]
	{
	    1,
	    40,
	    6,
	    700
	};

	// Output the header.
	Console.Write("Letter".PadRight(10));
	Console.WriteLine("Number".PadLeft(8));

	// Output array.
	foreach (int i in a)
	{
	    Console.Write("A".PadRight(10));
	    Console.WriteLine(i.ToString().PadLeft(8));
	}
    }
}

Output

Letter      Number
A                1
A               40
A                6
A              700

Questions

Question and answer

How can I use the pad methods on web pages? You can insert the padded text into a PRE tag, which stands for preformatted. This will render in the browsers as a text file, preserving your padding.

Can I use PadRight and PadLeft on string literals? Yes, as the string literals are objects you can call instance methods on. Look at the above examples to see the literals like "Letter" having PadRight called on it. String literals with padding are usually how you would make headers for your text tables. As shown, simply call PadRight or PadLeft on the string literal instance.

How is PadRight implemented in IL? PadRight and PadLeft are implemented in native code that is external to the IL you can inspect. The PadHelper method is internally called, and I can't see inside it.

Implementation of PadRight [C#]

public string PadRight(int totalWidth)
{
    return this.PadHelper(totalWidth, ' ', true);
}

Notes

Note

It is best to use padding for certain debugging output methods, but for more user-focused displays, having an HTML table or DataGridView would be much better. Padding is also be useful on certain text printouts: think of the movie theater receipts, or bank statements.

Microsoft Developer Network. MSDN states that PadRight "left-aligns the characters in this string, padding on the right with spaces or a specified Unicode character, for a specified total length." This means you can specify any Unicode character and expect correct results. However, in my experience the only useful padding characters are the dot, and spaces.

MSDN reference

Special characters for padding

You can use special characters, such as a dot or dash, for padding. For example, you could use PadRight and PadLeft with periods and connect a string with a right-aligned number.

Program that pads [C#]

using System;

class Program
{
    static void Main()
    {
	Console.Write("Sam".PadRight(10, '.'));
	Console.WriteLine("535".PadLeft(4, '.'));

	Console.Write("Jason".PadRight(10, '.'));
	Console.WriteLine("1".PadLeft(4, '.'));
    }
}

Output

Sam........535
Jason........1

Summary

The C# programming language

Here we saw how you can use the PadRight method in C# programs. Presentation layers in your applications are sometimes very complex, but sometimes complexity just gets in the way. In these situations, use padding to decrease complexity.

PadLeft String Method String Type
.NET