Open In App

C# | PadRight() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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: 

Csharp




// 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 than string length
        Console.WriteLine("Pad 20 :'{0}'", s1.PadRight(20));
    }
}


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: 

csharp




// 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 than string length
        Console.WriteLine("Pad 20 :'{0}'", s1.PadRight(20, pad));
    }
}


Output:

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

References: 



Last Updated : 02 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads