![]() |
VOOZH | about |
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.
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.
public string PadRight(int totalWidth)
public string PadRight(int totalWidth, char paddingChar);
Parameters:
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 paddingto add extra space on the right of a string.
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.
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).