Home »
.Net »
C# Programs
C# - String.PadRight() Method | Pad a String from Right
Learn, how to pad a given string from the right using C# program?
[Last updated : March 20, 2023]
To pad a string from the right, we use String.PadRight() method.
String.PadRight()
The String.PadRight() method returns padded string from right.
Syntax
String String.PadRight(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 right 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 end.
C# program to pad a string from right
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.PadRight(30, '#');
Console.WriteLine("String after right padding:(" + str2 + ")");
}
}
}
Output
String after right padding:(This is a sample string#######)
Explanation
In above program we used '#' character to pad string from right side, and string length after padding will be 30 characters.
C# Basic Programs »