C# | Left padding a string with spaces

This is a method of String Class

Syntax

string string.PadLeft(int totalWidth);

Program

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            string str = "Hello world";

            Console.WriteLine("Demonstration of PadLeft method");
            Console.WriteLine(str.PadLeft(str.Length + 1));
            Console.WriteLine(str.PadLeft(str.Length + 2));
            Console.WriteLine(str.PadLeft(str.Length + 3));
            Console.WriteLine(str.PadLeft(str.Length + 4));
            Console.WriteLine(str.PadLeft(str.Length + 5));
            
            Console.WriteLine();  
        }
    }
}

Output

Demonstration of PadLeft method
 Hello world
  Hello world
   Hello world
    Hello world
     Hello world

Leave a Comment

error: Alert: Content is protected!!