When we need to align output of strings we can use method String.PadLeft() or String.PadRight():
Sample:
string[] initialData = new string[]
{"one", "two",
"three", "four", "five"};
Console.WriteLine("PadLeft sample");
foreach (string item in initialData)
{
string processedItem = item.PadLeft(10, '.');
Console.WriteLine("'{0}'", processedItem);
}
Console.WriteLine("PadRight sample");
foreach (string item in initialData)
{
string processedItem = item.PadRight(10, '.');
Console.WriteLine("'{0}'", processedItem);
}
Console output:
Reference:
No comments:
Post a Comment