PadRight
, PadLeft
PadRight
adds spaces to the right of strings. PadLeft
meanwhile adds to the left. These C# methods make text easier to read.
Padding a string
adds whitespace or other characters to the beginning or end. Any character can be used for padding—not just spaces.
PadRight
This method can be used to create columns of text that are left-aligned. We add padding (on the right) up to the specified numbers of characters. This creates a column.
PadRight
method on the "value" string
. This expands, or pads, the width of the string
to 3 chars.using System; // Pad a string to 3 chars, and use Console methods to write it. string value = "x".PadRight(3); Console.Write(value); Console.WriteLine("y");x y
This next example is similar to the first one, but it uses a foreach
-loop to build up a columnar layout. It shows how to call PadRight
on string
literals.
using System; 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"); }Key Value cat noun poodle noun lizard noun frog noun photon noun
In many applications, numeric values are aligned to the right. This is how Excel aligns numbers. We see that the numbers on the right are aligned to the right edge.
string
data. The console calls are not required.using System; 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)); }Letter Number A 1 A 40 A 6 A 700
You can use special characters, such as a dot or dash, for padding. You could use PadRight
and PadLeft
with periods and connect a string
with a right-aligned number.
using System; Console.Write("Sam".PadRight(10, '.')); Console.WriteLine("535".PadLeft(4, '.')); Console.Write("Jason".PadRight(10, '.')); Console.WriteLine("1".PadLeft(4, '.'));Sam........535 Jason........1
PadLeft
Here the PadLeft
method is tested with two parameters. The first is the column width in characters and the second is the padding character.
PadLeft
can be used. We can control the number of decimal places with a call like ToString
"0.00."using System; string[] words = { "1", "200", "Samuel", "Perls" }; foreach (string word in words) // Loop over words. { // Write ten characters. Console.WriteLine(word.PadLeft(10, '_')); // Write ten characters with space. Console.WriteLine(word.PadLeft(10)); }_________1 1 _______200 200 ____Samuel Samuel _____Perls Perls
string
We can avoid PadRight
and PadLeft
completely and instead use a format string
. This can combine padding methods with other string
operations.
string
manipulations and concatenations, format strings can be easier to read and shorter.using System; string[] values = { "bird", "0", "1" }; foreach (string value in values) { // Use an argument to formatting string for padding. // ... Positive means "pad right." // ... Negative (not shown) means "pad left." string padded = string.Format("{0,5}", value); Console.WriteLine(padded); } bird 0 1
String
literalsThese are string
objects, and we can pad them. String
literals like "Word" can have PadRight
called on them. Literals with padding can make headers for our text tables.
We can improve performance with a cache of the padded strings—in a Dictionary
or array. This is a form of precomputed lookup tables.
StringBuilder
to write padding might be a better choice.StringBuilder
provides no padding methods. But you can write a certain number of spaces into it, creating padding with no allocation.We used the PadRight
and PadLeft
methods. Padding methods are helpful when developing columnar layouts of numeric data, as with decimal places.