Thursday, April 24, 2008

CSharp (c#) methods String.PadLeft(), String.PadRight().

    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:



padConsole



Reference:



String.PadLeft Method



String.PadRight Method

No comments: