Languages
[Edit]
EN

C# / .NET - string padding left

0 points
Created by:
Kelly
394

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

References

  1. String.PadLeft Method (System) | Microsoft Docs
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join