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# 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# | TrimStart() and TrimEnd() Method
Prerequisite: Trim() Method in C# In C#, TrimStart() & TrimEnd() are the string methods. TrimStart() method is used to remove the occurrences of a set of characters specified in an array from the starting of the current String object. TrimEnd() method is used to remove the occurrences of a set o
3 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# | CharEnumerator.MoveNext() Method
CharEnumerator.MoveNext() Method is used to increments the internal index of the current CharEnumerator object to the next character of the enumerated string. Syntax: public bool MoveNext(); Return Value: This method returns the boolean value true value if the index is successfully incremented and w
2 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# | Char.GetNumericValue() Method
In C#, Char.GetNumericValue() is a System.Char struct method which is used to convert a numeric Unicode character into a double-precision floating-point number. The numeric value must belong to UnicodeCategory, i.e DecimalDigitNumber, LetterNumber, or OtherNumber. This method can be overloaded by pa
2 min read
String C/C++ Programs
C program to swap two StringsC Program to Sort an array of names or stringsC Program to Check if a Given String is PalindromeC/C++ Program for Return maximum occurring character in the input stringC/C++ Program for Remove all duplicates from the input string.C/C++ Program for Print all the duplicate
3 min read
C Program to Find the Length of a String
The length of a string is the number of characters in it without including the null character (â\0â). In this article, we will learn how to find the length of a string in C. The easiest way to find the string length is by using strlen() function from the C strings library. Let's take a look at an ex
2 min read