Home »
.Net »
C# Programs
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.
String.PadLeft()
The String.PadLeft() method returns padded string from left.
Syntax
String String.PadLeft(int totalLength, char ch);
Parameter(s)
- 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.
Example
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 + ")");
}
}
}
Output
String after left padding:(#######This is a sample string)
Explanation
In above program we used ‘#’ character to pad string from left, and string length after padding will be 30 characters.
C# Basic Programs »