C# | PadRight() Method

In C#, PadRight() is a string method. This method is used to left-aligns the characters in String by padding them with spaces or specified character on the right, for a specified total length. This method can be overloaded by passing different parameters to it.

  • String.PadRight Method(Int32)
  • String.PadRight Method(Int32, Char)

String.PadRight Method(Int32)

This method is used to left-aligns the characters in this string by padding them with spaces on the right. The parameter “totalWidth” will specify the number of padding characters in the string and this method will return a new string.



Syntax:

public string PadRight(int totalWidth)
  • Parameter: This method will accept one parameter “totalWidth” which specify the number of padding characters in string. The type of this parameter is System.Int32.
  • Return Value: This method will return the string which pads the left portion of the string. The return value type is System.String.

Exception: If totalWidth is less than zero then it will arise ArgumentOutOfRangeException.

Example:

Input : str  = "GeeksForGeeks"
        str.PadRight(2);
Output: 'GeeksForGeeks' 

// String is same because totalWidth 
// is less than length of String.

Input : str  = "GeeksForGeeks"
        str.PadRight(13);
Output: 'GeeksForGeeks' 

// String is same because of totalWidth 
// is equal to the length of String.

Input : str  = "GeeksForGeeks"
        str.PadRight(20);
Output: 'GeeksForGeeks       '   

// String is changed because of totalWidth
// is greater than the length of String.

So Right Padding will show only if the 
totalWidth is greater than string length.

Below program illustrate the above-discussed method:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// String.PadRight(totalWidth) method
using System;
class Geeks {
  
    // Main Method
    public static void Main()
    {
        string s1 = "GeeksForGeeks";
  
        Console.WriteLine("String : " + s1);
  
        // totalwidth is less than string length
        Console.WriteLine("Pad 2 :'{0}'", s1.PadRight(2));
  
        // totalwidth is equal to string length
        Console.WriteLine("Pad 13 :'{0}'", s1.PadRight(13));
  
        // totalwidth is greater then string length
        Console.WriteLine("Pad 20 :'{0}'", s1.PadRight(20));
    }
}

chevron_right


Output:

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

String.PadRight Method (Int32, Char)

This method is used to left-aligns the characters in this string by padding them with specified character on the right. The parameter “totalWidth” will specify the number of padding characters in string and “paddingChar” is the specified Character.

Syntax:

public string PadRight(int totalWidth, char paddingChar)
  • Parameter: This method accept two parameter “totalWidth” and “paddingChar“. The parameter “totalWidth” will specify the number of padding characters in string and type of this parameter is System.Int32. The parameter “paddingChar” will specify the padding character and type of this parameter is System.Char.
  • Return Value: This method will return a new string which will be equivalent to current string, but left-aligned and padded on the right with the characters specified by “paddingChar” parameter. If totalWidth is less than the length of string, the method returns the same string. If totalWidth is equal to the length of the string, the method returns a new string that is identical to current String. The return value type is System.String.

Exception: If totalWidth is less than zero then it will arise ArgumentOutOfRangeException.

Example:

Input : str  = "GeeksForGeeks"
        str.PadRight(2, '*');
Output: 'GeeksForGeeks' 

// String is same because totalWidth
// is less than the length of String.

Input : str  = "GeeksForGeeks"
        str.PadRight(13, '*');
Output: 'GeeksForGeeks' 

// String is same because of totalWidth
// is equal to the length of String.

Input : str  = "GeeksForGeeks"
        str.PadRight(20, '*');
Output: 'GeeksForGeeks*******'   

// String is changed because totalWidth 
// is greater than the length of String.

Below program illustrate the above-discussed method:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// String.PadRight(int totalWidth, 
// char paddingChar) method 
using System;
class Geeks {
  
    // Main Method
    public static void Main()
    {
        string s1 = "GeeksForGeeks";
        char pad = '*';
        Console.WriteLine("String : " + s1);
  
        // totalwidth is less than string length
        Console.WriteLine("Pad 2 :'{0}'", s1.PadRight(2, pad));
  
        // totalwidth is equal to string length
        Console.WriteLine("Pad 13 :'{0}'", s1.PadRight(13, pad));
  
        // totalwidth is greater then string length
        Console.WriteLine("Pad 20 :'{0}'", s1.PadRight(20, pad));
    }
}

chevron_right


Output:

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

References:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.