How to pad string from left using String.PadLeft() in C#?

String.PadLeft() Method of String class in C#: Here, we will learn how to pad string from left with specific characters in C#?

Given a string and we have to pad the string from left with specific characters.


Method returns padded string from left.


String String.PadLeft(int totalLength, char ch);

totalLength : This parameter specifies total length of string after padding.
ch : This parameter specifies a character which will be used to pad string from left side.


Input string: "This is a sample string"
Padding string with '#' and total string length will be 30
Output string: "#######This is a sample string"

Explanation: Input string's length was 23 and to make it 30, program added 7 characters (#) in the starting.

Consider the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
    class Program
        static void Main()
            String str1 = "This is a sample string";
            String str2;
            str2 = str1.PadLeft(30, '#');

            Console.WriteLine("String after left padding:(" + str2+")");



String after left padding:(#######This is a sample string)

In above program we used ‘#’ character to pad string from left, and string length after padding will be 30 characters.

C# Basic Programs »


What's New (MCQs)

Comments and Discussions!

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

© some rights reserved.