C# String PadRight() Method
Last Updated :
12 Mar, 2025
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}'");
}
}
OutputOriginal 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));
}
}
OutputString : 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));
}
}
OutputString : 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).
Similar Reads
C# String PadLeft() Method
The PadLeft() method in C# is a method of the String class. This method is very useful when we want to right-align the string by adding an extra space or padding on the left or the start of the string. It is used to format the text and modify its appearance. This method can be overloaded by passing
4 min read
C# | Char.ToString() Method
In C#, Char.ToString() is a System.Char struct method which is used to convert the value of this instance to its equivalent string representation. This method can be overloaded by passing different type of arguments to it. Char.ToString(IFormatProvider) Method Char.ToString(Char) Method Char.ToStrin
2 min read
C# | Trim() Method
C# Trim() is a string class method. This method is used to remove all leading and trailing white-space characters from the current String object and return a new modified string. This method is beneficial for clearing the unwanted extra spaces from the string. Example 1: Using the Trim() method to r
4 min read
C# String Insert() Method
The Insert() method in C# is a part of the String class. It is used to insert a new string at a specified index position and return a new string with the inserted string value. This method does not make the changes in the original string because strings are immutable in C#. It returns a new modified
3 min read
C# String Properties
In C#, a String is an array of characters. The string class represents the text as a series of Unicode characters. It provides various properties and methods so that it becomes easy to work with strings. There are two properties in the string class: Chars[Int32]: Used to get the Char object at a spe
4 min read
C# | ToCharArray() Method
In C#, ToCharArray() is a string method. This method is used to copy the characters from a specified string in the current instance to a Unicode character array or the characters of a specified substring in the current instance to a Unicode character array. This method can be overloaded by changing
4 min read
C# Program to Reverse a String without using Reverse() Method
C# has a built-in function to reverse a string. First, the string is converted to a character array by using ToCharArray() then by using the Reverse() the character array will be reversed, But in this article, we will understand how to Reverse a String without using Reverse(). Example Input : Geek O
2 min read
C# | CopyTo() Method
In C#, the CopyTo() is a method of String Class. It is used to copy a specified number of characters from a specified position in the string and it copies the characters of this string into an array of Unicode characters. Example 1: Using the CopyTo() method to copy characters from a string to a cha
3 min read
How to Pad an Integer Number With Leading Zeroes in C#?
Given a Number N, the task is to pad this number with P number of leading zeros in C#. Examples: Input: N = 123 , P = 4 Output: 0123 Input: N = -3 , P = 5 Output: -00003 Method 1: Using string Format() method : The Format() method is used to replace one or more format items in the specified string w
4 min read
C# Program to Display the Abbreviation of a Text
Given a text, we need to display the Abbreviation of the text. An abbreviation is the shortest form of any word or phrase. It contains a group of letters that takes from the full form of the word or phrase. For example, GeeksforGeeks abbreviation is GFG, or the abbreviation of Advance Data Structure
2 min read