Padding C# String

In this article we will discuss about how to use PadLeft and PadRight method in C#.
  • 3750

The string class provides PadLeft and PadRight methods to pad a string.

Left padding a string in C#

The PadLeft method of the string class is used apply left padding on a string. The following code snippet uses PadLeft method to pad a string.

string name = "Mahesh Chand";
char pad = '*';
Console.WriteLine(name.PadLeft(15, pad));
Console
.WriteLine(name.PadLeft (2));  

Right padding a string in C#

The PadRight method of the string class is used apply left padding on a string. The following code snippet uses PadRight method to pad a string.

string name = "Mahesh Chand";
char pad = '*';
Console.WriteLine(name.PadLeft(15, pad));
Console.WriteLine(name.PadRight(2, pad));

Ask Your Question 

Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.