C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Part 1: We invoke the PadRight method on the "cat" string. This expands, or pads, the width of the string to 10 chars.
Part 2: We call PadRight on a different string. Because we used Console.WriteLine, the new string is written on a separate line.
ConsoleTip: Padding strings is most useful for console programs, debug output, or certain text formats.
C# program that uses PadRight
using System;
class Program
{
static void Main()
{
// Part 1: pad a string to 10 chars, and use Console methods to write it.
string cat = "cat".PadRight(10);
Console.Write(cat);
Console.WriteLine("feline");
// Part 2: repeat the same steps.
string dog = "poodle".PadRight(10);
Console.Write(dog);
Console.WriteLine("canine");
}
}
Output
cat feline
poodle canine
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
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
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
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
Format: 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.FormatC# program that uses padding format string
using System;
class Program
{
static void Main()
{
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);
}
}
}
Output
bird
0
1
Implementation of PadRight: C#
public string PadRight(int totalWidth)
{
return this.PadHelper(totalWidth, ' ', true);
}
Web pages: You can insert the padded text into a PRE element (preformatted). This will render in browsers as a text file.
HTML, XMLAnd: This means we can improve performance with a cache of the padded strings—in a Dictionary or array.
DictionaryArrayHowever: 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.
StringBuilder