Home » C#

C# | Left padding a string with spaces

Example of String.PadLeft() method in C#: Here, we are going to learn how to left pad a string with spaces in C#?
Submitted by IncludeHelp, on November 15, 2019

PadLeft() method is a library method of the String class. It is used to pad the string from the left side with spaces.

Syntax:

    string string.PadLeft(int totalWidth);

Here, totalWidth is the total number of characters of the string, if it is more than the length of the string, string will be padded with spaces, method returns the space padded string.

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.