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:
xxxxxxxxxx
1
string text = "Example text";
2
3
Console.WriteLine(text.PadLeft(15)); // Output: " Example text"
In this example, we use PadLeft()
method to get string padded with spaces or with a specified Unicode character.
xxxxxxxxxx
1
using System;
2
using System.Linq;
3
4
public class Program
5
{
6
public static void Main()
7
{
8
string text = "Example text";
9
10
Console.WriteLine(text.PadLeft(15));
11
Console.WriteLine(text.PadLeft(5));
12
Console.WriteLine(text.PadLeft(15, '*'));
13
}
14
}
Output:
xxxxxxxxxx
1
Example text
2
Example text
3
***Example text