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.
Info We invoke the PadRight method on the "value" string. This expands, or pads, the width of the string to 3 chars.
Tip Padding strings is most useful for console programs, debug output, or certain text formats.
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
Example 2. 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.
Note The output of the example will be a simple table with two columns, similar to the previous example.
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
Example 3. 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.
Note The same logic can be used to create padding in 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
Chars. 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.
Note 2 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
Format string. We can avoid PadRight and PadLeft completely and instead use a format string. This can combine padding methods with other string operations.
Detail We use a comma, and then a negative or positive integer for padding. Positive means right padding, and negative means left padding.
Tip If we combine multiple 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 literals. These 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.
A summary. We used the PadRight and PadLeft methods. Padding methods are helpful when developing columnar layouts of numeric data, as with decimal places.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.