C# PadRight and PadLeft: String Columns

Use the PadRight and PadLeft methods. PadRight adds spaces to the right of strings.

PadRight, PadLeft.

PadRight adds spaces to the right of strings. PadLeft meanwhile adds to the left. These methods make text easier to read.


a string adds whitespace or other characters to the beginning or end. Any character can be used for padding—not just spaces.Char


We use the PadRight method to right align strings. The string literals have padding applied on the right side. The resulting strings are 10 characters. This creates a column.String Literal

Info: On both strings, the PadRight method is called. This expands, or pads, the width of the string to 10 chars.

Int, uintConsole.WriteLine

Tip: Padding strings is most useful for Console apps, debug output, or certain text formats.

C# program that uses PadRight 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 cat feline poodle canine

Example 2.

This next example is similar to the first one, but it uses a foreach-loop to build up a columnar, preformatted 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.

C# program that uses foreach loop, PadRight 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

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.

C# program that uses PadRight and PadLeft 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


It is best to use padding for debugging methods. For more user-focused displays, having an HTML table or DataGridView is better. Padding is also useful on text printouts.DataGridView

Web pages: You can insert the padded text into a PRE element (preformatted). This will render in browsers as a text file.


String literals.

These are string objects. You can pad them. Above, string literals like "Letter" have PadRight called on them. Literals with padding can make headers for your text tables.


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.IL
Implementation of PadRight: C# public string PadRight(int totalWidth) { return this.PadHelper(totalWidth, ' ', true); }


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.
C# program that uses char for padding 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


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.

Overload: The padding methods are overloaded. We can call different "forms" of the methods with different arguments.

Decimal places: PadLeft can be used. You can control the number of decimal places with a call like value.ToString("0.00").

C# program that uses PadLeft using System; class Program { static void Main() { 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)); } } } Output _________1 1 _______200 200 ____Samuel Samuel _____Perls Perls


The PadLeft and PadRight methods call into a PadHelper method. The code is written in a native computer language and is optimized. But it still must create a new string.

And: This means we can improve performance with a cache of the padded strings—in a Dictionary or array.


However: Optimizing padding is often a low-value optimization. Using a StringBuilder to write padding might be a better choice.

StringBuilder: This 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. Presentation layers in applications are sometimes complex. And sometimes complexity just gets in the way.

In these situations,

we can use padding to decrease complexity. Padding methods are helpful when developing columnar layouts of numeric data, as with decimal places.
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen,