EN
C# / .NET - string padding left
0
points
In this article, we would like to show you how to pad left indicated string in C#.
Quick solution:
string text = "Example text";
Console.WriteLine(text.PadLeft(15)); // Output: " Example text"
Practical example
In this example, we use PadLeft() method to get string padded with spaces or with a specified Unicode character.
using System;
using System.Linq;
public class Program
{
public static void Main()
{
string text = "Example text";
Console.WriteLine(text.PadLeft(15));
Console.WriteLine(text.PadLeft(5));
Console.WriteLine(text.PadLeft(15, '*'));
}
}
Output:
Example text
Example text
***Example text