Home »
C#
C# | Right padding a string with spaces
Example of String.RightPad() method in C#: Here, we are going to learn how to right pad a string with spaces in C#?
Submitted by IncludeHelp, on November 15, 2019
RightPad() method is a library method of the String class. It is used to pad the string from the right side with spaces.
Syntax:
string string.PadRight(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 PadRight method");
Console.WriteLine(str.PadRight(str.Length + 1) + "##########");
Console.WriteLine(str.PadRight(str.Length + 2) + "##########");
Console.WriteLine(str.PadRight(str.Length + 3) + "##########");
Console.WriteLine(str.PadRight(str.Length + 4) + "##########");
Console.WriteLine(str.PadRight(str.Length + 5) + "##########");
Console.WriteLine();
}
}
}
Output
Demonstration of PadRight method
Hello world ##########
Hello world ##########
Hello world ##########
Hello world ##########
Hello world ##########