Open In App

C# String PadRight() Method

Last Updated : 12 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, the PadRight() is a method of a String class. This method is used to left-align the characters in a String by padding them with spaces or specified characters on the right for a specified total length.

Example 1: Using the PadRight() method to add padding to the right of a string.

C#
// C# program to illustrate the
// String.PadRight(Int32) Method

using System;
class Geeks
{
    public static void Main(String[] args)
    {  
        // Creating a string
        string s = "Geeks";

        // Using the PadRight(Int32) Method
        // To add extra whitespace in the right
        String res = s.PadRight(10);

        // String after adding padding
        Console.WriteLine($"Original string: '{s}'");

        // Using the PadRight(totalWidth, paddingChar) Method
        Console.WriteLine($"String after adding padding: '{res}'");
        
        // To add extra characters to right of the string
        res = s.PadRight(10, '-');
        Console.WriteLine($"String after adding padding: '{res}'");
    }
}

Output
Original string: 'Geeks'
String after adding padding: 'Geeks     '
String after adding padding: 'Geeks-----'

Explanation: In the above example, we use the PadRight() method to add the extra padding on the right of the string first we pass it without the specified character so by default it adds the extra white space in the string and then we pass the specified character in the argument which added the extra characters in the right of the string.

Syntax of String PadRight() Method

public string PadRight(int totalWidth)

public string PadRight(int totalWidth, char paddingChar);

Parameters:

  • totalWidth: The desired total length of the resulting string, including the original string and padding characters.
  • paddingChar (optional): The character to use for padding. If this parameter is omitted, the default padding character is a space (‘ ‘).

Return Type: The method pads the right portion of the string, not the left The return value type is System.String.

Exceptions: ArgumentOutOfRangeException: If totalWidth is less than zero for example -1 or -30.


Example 2: Using PadRight(totalWidth) with different padding to add extra space on the right of a string.

C#
// C# program to illustrate the
// String.PadRight(totalWidth) method
using System;
class Geeks 
{
	public static void Main()
	{
		string s = "GeeksForGeeks";

		Console.WriteLine("String : " + s);

		// totalwidth is less than string length
		Console.WriteLine("Pad 2 :'{0}'", s.PadRight(2));

		// totalwidth is equal to string length
		Console.WriteLine("Pad 13 :'{0}'", s.PadRight(13));

		// totalwidth is greater than string length
		Console.WriteLine("Pad 20 :'{0}'", s.PadRight(20));
	}
}

Output
String : GeeksForGeeks
Pad 2 :'GeeksForGeeks'
Pad 13 :'GeeksForGeeks'
Pad 20 :'GeeksForGeeks       '

Explanation: In the above code example, we use the PadRight(totalWidth) method to add the extra space on the right of the string. In the code we can see that if the padding is less than the length of the string it will return the identical string otherwise it will add the padding on the right of the string.


Example 3: Using PadRight(int totalWidth, char paddingChar) to the padding character on the right of a string.

C#
// C# program to illustrate the
// String.PadRight(int totalWidth, 
// char paddingChar) method 

using System;
class Geeks 
{
	public static void Main()
	{
		string s = "GeeksForGeeks";
		char pad = '*';
		Console.WriteLine("String : " + s);

		// totalwidth is less than string length
		Console.WriteLine("Pad 2 :'{0}'", s.PadRight(2, pad));

		// totalwidth is equal to string length
		Console.WriteLine("Pad 13 :'{0}'", s.PadRight(13, pad));

		// totalwidth is greater than string length
		Console.WriteLine("Pad 20 :'{0}'", s.PadRight(20, pad));
	}
}

Output
String : GeeksForGeeks
Pad 2 :'GeeksForGeeks'
Pad 13 :'GeeksForGeeks'
Pad 20 :'GeeksForGeeks*******'

Explanation: In the above code example, we use the PadRight(int totalWidth, char paddingChar) method and pass the specific character (*) in the argument to add extra characters to the right of the string and similarly we can see that if the length of the string is less than the specified string it will return the new identical string(System.String).



Next Article
Article Tags :

Similar Reads