SoFunction
Updated on 2025-03-01

Specific use of methods in C#

Returns a new string of the specified length, with spaces or specified Unicode characters at the end of the current string.

1. Reload

PadRight(Int32) Returns a new string that reaches the specified total length by filling the right of the characters in this string with spaces, thus making the characters left aligned.
PadRight(Int32, Char) Returns a new string that reaches the specified total length by filling the specified Unicode characters to the right of the character in this string, thereby making the characters left aligned.

2. PadRight(Int32)

Returns a new string that reaches the specified total length by filling the right of the characters in this string with spaces, thus making the characters left aligned.

1. Definition

public string PadRight (int totalWidth);

parameter
totalWidth
Int32
Number of characters in the result string,Equal to the original number of characters plus any other padding characters。

return
String
A new string equivalent to this instance,But the string is left aligned,therefore,Fill as many spaces as you want on the right,Make the length reach totalWidth。 but,if totalWidth Less than the length of this instance,则此方法return对现有实例的引用。 if totalWidth equals the length of this instance,则此方法return与此实例相同的新字符串。

exception
ArgumentOutOfRangeException
totalWidth Less than zero。

2. Example

// PadRight(Int32)

namespace ConsoleApp46
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            (args);

            string str;
            str = "BBQ and Slaw";

            ("|");
            ((15));
            ("|");       // Length = 15
            ("|");
            ((5));
            ("|");       // Length = length of existing instance        }
    }
}
//Run result:/*
|BBQ and Slaw   |
|BBQ and Slaw|
 */

Unicode space is defined as hexadecimal 0x0020.

The PadRight(Int32) method fills the end of the returned string. This means that when used with a right-to-left language, it fills the left part of the string.

3. PadRight(Int32, Char)

Returns a new string that reaches the specified total length by filling the right of the characters in this string with the specified Unicode characters, thus making the characters left aligned.

1. Definition

public string PadRight (int totalWidth, char paddingChar);

parameter
totalWidth
Int32
Number of characters in the result string,Equal to the original number of characters plus any other padding characters。

paddingChar
Char
Unicode Fill characters。

return
String
A new string equivalent to this instance,But the string is left aligned,therefore,Fill as many as you want on the right paddingChar character,Make the length reach totalWidth。 but,if totalWidth Less than the length of this instance,则此方法return对现有实例的引用。 if totalWidth equals the length of this instance,则此方法return与此实例相同的新character串。

exception
ArgumentOutOfRangeException
totalWidth Less than zero。

2. Example

// PadRight(Int32, Char)
namespace ConsoleApp47
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            (args);

            string str = "forty-two";
            char pad = '.';

            ((15, pad));   // Use less than 15 seconds to make up for the example length.            ((2, pad));    // The instance length is greater than 2 to display the instance        }
    }
}
//Run result:/*
forty-two......
forty-two
 */

PadRight(Int32, Char) method fills the end of the returned string. This means that when used with a right-to-left language, it fills the left part of the string.

This is the end of this article about the specific use of methods in C#. For more related C# content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!