Home
C#
String PadRight and PadLeft
This page was last reviewed on Jan 4, 2024.
Dot Net Perls
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.
char
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.
Console.Write
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.
foreach
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.
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.
Then It is then tested with a single parameter—this yields a padding character of a space.
Note The padding methods are overloaded. We can call different forms of the methods with different arguments.
Overload
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.
string.Format
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.
String Literal
Optimization. We can improve performance with a cache of the padded strings—in a Dictionary or array. This is a form of precomputed lookup tables.
Dictionary
Array
However Optimizing padding is often a low-value optimization. Using a StringBuilder to write padding might be a better choice.
Info StringBuilder provides no padding methods. But you can write a certain number of spaces into it, creating padding with no allocation.
StringBuilder
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.
This page was last updated on Jan 4, 2024 (edit).
Home
Changes
© 2007-2024 Sam Allen.