C# - String.PadLeft() Method | Pad a String from Left

Learn, how to pad a given string from the left using C# program?
[Last updated : March 20, 2023]

To pad a string from the left, we use String.PadLeft() method.


The String.PadLeft() 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"

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

C# program to pad a string from left

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 »

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.