String.PadRight 方法

定義

傳回指定之長度的新字串,其中目前字串的結尾將以空白和或指定的 Unicode 字元填補。Returns a new string of a specified length in which the end of the current string is padded with spaces or with a specified Unicode character.

多載

PadRight(Int32, Char)

傳回新字串,此字串會以指定的 Unicode 字元填補右側至指定的總長度,靠左對齊這個字串中的字元。Returns a new string that left-aligns the characters in this string by padding them on the right with a specified Unicode character, for a specified total length.

PadRight(Int32)

傳回新字串,此字串會以空格填補右側至指定的總長度,靠左對齊這個字串中的字元。Returns a new string that left-aligns the characters in this string by padding them with spaces on the right, for a specified total length.

PadRight(Int32, Char)

傳回新字串,此字串會以指定的 Unicode 字元填補右側至指定的總長度,靠左對齊這個字串中的字元。Returns a new string that left-aligns the characters in this string by padding them on the right with a specified Unicode character, for a specified total length.

public:
 System::String ^ PadRight(int totalWidth, char paddingChar);
public string PadRight (int totalWidth, char paddingChar);
member this.PadRight : int * char -> string
Public Function PadRight (totalWidth As Integer, paddingChar As Char) As String

參數

totalWidth
Int32

產生的字串中的字元數,等於原始字元加上任何其他填補字元的數目。The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.

paddingChar
Char

Unicode 填補字元。A Unicode padding character.

傳回

與這個執行個體相等的新字串,但為靠左對齊,並在右側視需要填補若干 paddingChar 字元來建立 totalWidth 的長度。A new string that is equivalent to this instance, but left-aligned and padded on the right with as many paddingChar characters as needed to create a length of totalWidth. 但是,如果 totalWidth 小於這個執行個體的長度,此方法會傳回現有執行個體的參考。However, if totalWidth is less than the length of this instance, the method returns a reference to the existing instance. 如果 totalWidth 等於這個執行個體的長度,此方法會傳回等於這個執行個體的新字串。If totalWidth is equal to the length of this instance, the method returns a new string that is identical to this instance.

例外狀況

totalWidth 小於零。totalWidth is less than zero.

範例

下列範例示範 PadRight 方法。The following example demonstrates the PadRight method.

String^ str = "forty-two";
Console::Write( "|" );
Console::Write( str->PadRight( 15, '.' ) );
Console::WriteLine( "|" ); // Displays "|forty-two......|".
Console::Write( "|" );
Console::Write( str->PadRight( 5, '.' ) );
Console::WriteLine( "|" ); // Displays "|forty-two|".
string str = "forty-two";
char pad = '.';

Console.WriteLine(str.PadRight(15, pad));    // Displays "forty-two......".
Console.WriteLine(str.PadRight(2,  pad));    // Displays "forty-two".
Dim str As String
Dim pad As Char
str = "forty-two"
pad = Convert.ToChar(".") 
Console.WriteLine(str.PadRight(15, pad)) ' Displays "|forty-two......|".
Console.WriteLine(str.PadRight(2,  pad)) ' Displays "|forty-two|".

備註

PadRight(Int32, Char) 方法會填補傳回之字串的結尾。The PadRight(Int32, Char) method pads the end of the returned string. 這表示,當與由右至左的語言搭配使用時,它會填補字串的左邊部分。This means that, when used with right-to-left languages, it pads the left portion of the string.

注意

如果 PadRight 方法使用空白字元來填補目前的實例,這個方法就不會修改目前實例的值。If the PadRight method pads the current instance with white-space characters, this method does not modify the value of the current instance. 相反地,它會傳回新的字串,並以尾端的 paddingChar 字元填補,使其總長度為 totalWidth 字元。Instead, it returns a new string that is padded with trailing paddingChar characters so that its total length is totalWidth characters.

另請參閱

PadRight(Int32)

傳回新字串,此字串會以空格填補右側至指定的總長度,靠左對齊這個字串中的字元。Returns a new string that left-aligns the characters in this string by padding them with spaces on the right, for a specified total length.

public:
 System::String ^ PadRight(int totalWidth);
public string PadRight (int totalWidth);
member this.PadRight : int -> string
Public Function PadRight (totalWidth As Integer) As String

參數

totalWidth
Int32

產生的字串中的字元數,等於原始字元加上任何其他填補字元的數目。The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.

傳回

與這個執行個體相等的新字串,但為靠左對齊,並在右側視需要填補若干空間來建立 totalWidth 的長度。A new string that is equivalent to this instance, but left-aligned and padded on the right with as many spaces as needed to create a length of totalWidth. 但是,如果 totalWidth 小於這個執行個體的長度,此方法會傳回現有執行個體的參考。However, if totalWidth is less than the length of this instance, the method returns a reference to the existing instance. 如果 totalWidth 等於這個執行個體的長度,此方法會傳回等於這個執行個體的新字串。If totalWidth is equal to the length of this instance, the method returns a new string that is identical to this instance.

例外狀況

totalWidth 小於零。totalWidth is less than zero.

範例

下列範例示範 PadRight 方法。The following example demonstrates the PadRight method.

String^ str = "BBQ and Slaw";
Console::Write( "|" );
Console::Write( str->PadRight( 15 ) );
Console::WriteLine( "|" ); // Displays "|BBQ and Slaw   |".
Console::Write( "|" );
Console::Write( str->PadRight( 5 ) );
Console::WriteLine( "|" ); // Displays "|BBQ and Slaw|".
string str;
str = "BBQ and Slaw";

Console.Write("|");
Console.Write(str.PadRight(15));
Console.WriteLine("|");       // Displays "|BBQ and Slaw   |".

Console.Write("|");
Console.Write(str.PadRight(5));
Console.WriteLine("|");       // Displays "|BBQ and Slaw|".
Dim str As String
str = "BBQ and Slaw"

Console.Write("|")
Console.Write(str.PadRight(15))
Console.WriteLine("|") ' Displays "|BBQ and Slaw   |".

Console.Write("|")
Console.Write(str.PadRight(5))
Console.WriteLine("|") ' Displays "|BBQ and Slaw|".

備註

Unicode 空間會定義為十六進位0x0020。A Unicode space is defined as hexadecimal 0x0020.

PadRight(Int32) 方法會填補傳回之字串的結尾。The PadRight(Int32) method pads the end of the returned string. 這表示,當與由右至左的語言搭配使用時,它會填補字串的左邊部分。This means that, when used with right-to-left languages, it pads the left portion of the string.

注意

如果 PadRight 方法使用空白字元來填補目前的實例,這個方法就不會修改目前實例的值。If the PadRight method pads the current instance with white-space characters, this method does not modify the value of the current instance. 相反地,它會傳回新的字串,並以尾端空白字元填補,使其總長度為 totalWidth 個字元。Instead, it returns a new string that is padded with trailing white space so that its total length is totalWidth characters.

另請參閱

適用於