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 d
4 min read
Java String trim() Method
The trim() method of the String class in Java is used to remove leading and trailing whitespace from a string. The Unicode value of the space character is "\u0020". It does not remove any whitespace in the middle of the string. This method returns a new string with the whitespace removed and leaves
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 spec
4 min read
string erase in C++
The string erase() function is a built-in function of the string class that is used to erase the whole or part of the string, shortening its length. The function provides different ways to delete a portion of a string based on either an index position or a range of characters or delete the whole str
4 min read
std::string::insert() in C++
In C++, the string::insert() function is used insert the characters or a string at the given position of the string. It is the member function of std::string class.The string::insert method can be used in the following ways to:Table of ContentInsert a Single CharacterInsert a Single Character Multip
4 min read
Left Rotation of a String
Given a string s and an integer d, the task is to left rotate the string by d positions.Examples:Input: s = "GeeksforGeeks", d = 2Output: "eksforGeeksGe" Explanation: After the first rotation, string s becomes "eeksforGeeksG" and after the second rotation, it becomes "eksforGeeksGe".Input: s = "qwer
15+ min read
Strings in C
A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i
5 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
Strings in C++ and How to Create them?
Strings in C++ are used to store text or sequences of characters. In C++ strings can be stored in one of the two following ways: C-style string (using characters)String class Each of the above methods is discussed below: 1. C style string: In C, strings are defined as an array of characters. The dif
2 min read