C# String PadRight()

The PadRight() method in C# is a string method that allows you to pad the right side of a string with a specified character or a specified number of characters. The method returns a new string that is equivalent to the original string with the padding applied.

Here is the syntax for using the PadRight() method:

public string PadRight(int totalWidth);
public string PadRight(int totalWidth, char paddingChar);

The totalWidth parameter specifies the total length of the resulting string, including the original string and any additional padding characters. If the original string is already longer than totalWidth, the method will return the original string without any modifications.

The paddingChar parameter (optional) specifies the character to use for padding. If you don’t provide this parameter, the method will pad the string with spaces.

Here’s an example that demonstrates the usage of PadRight():

string originalString = "Hello";
int totalWidth = 10;

string paddedString = originalString.PadRight(totalWidth);
Console.WriteLine(paddedString);  // Output: "Hello     "

char paddingChar = '*';
string customPaddedString = originalString.PadRight(totalWidth, paddingChar);
Console.WriteLine(customPaddedString);  // Output: "Hello*****"

In the first example, the original string “Hello” is padded with spaces to achieve a total width of 10 characters.

In the second example, the original string “Hello” is padded with asterisks (*) to achieve a total width of 10 characters.

Note that the PadRight() method does not modify the original string; it returns a new string with the padding applied.

Parameter:

The PadRight() method in C# has two parameters:

  1. totalWidth (required): It specifies the total length of the resulting string, including the original string and any additional padding characters. If the original string is already longer than totalWidth, the method will return the original string without any modifications.
  2. paddingChar (optional): It specifies the character to use for padding. This parameter is optional, and if you don’t provide it, the method will pad the string with spaces.

Here is the syntax for the PadRight() method:

public string PadRight(int totalWidth);
public string PadRight(int totalWidth, char paddingChar);

To use the method, you need to provide at least the totalWidth parameter, while the paddingChar parameter is optional.

Return:

The PadRight() method in C# returns a new string that is equivalent to the original string with the padding applied. The resulting string will have a length equal to the specified totalWidth, including the original string and any additional padding characters.

If the original string is already longer than the totalWidth parameter, the method will return the original string without any modifications. Otherwise, if padding is required, the method will return a new string with the original string followed by the specified padding characters.

Here’s an example that demonstrates the return value of the PadRight() method:

string originalString = "Hello";
int totalWidth = 10;

string paddedString = originalString.PadRight(totalWidth);
Console.WriteLine(paddedString);  // Output: "Hello     "

In this example, the PadRight() method pads the original string “Hello” with spaces to achieve a total width of 10 characters. The resulting string “Hello ” is then assigned to the paddedString variable, and it is also printed to the console.

It’s important to note that the PadRight() method does not modify the original string. Instead, it returns a new string with the padding applied.

C# String PadRight() Method Example:

Certainly! Here’s an example that demonstrates the usage of the PadRight() method in C#:

string originalString = "Hello";
int totalWidth = 10;

string paddedString = originalString.PadRight(totalWidth);
Console.WriteLine(paddedString);  // Output: "Hello     "

In this example, we have an original string “Hello” and a totalWidth of 10. The PadRight() method is called on the originalString with the totalWidth as the argument.

The method pads the right side of the string with spaces to achieve a total width of 10 characters. The resulting string “Hello ” (with 5 additional spaces) is assigned to the paddedString variable. Finally, the paddedString is printed to the console, resulting in the output: “Hello “.

You can experiment with different values for originalString and totalWidth to observe the effects of the PadRight() method and the resulting padded strings.