String.PadRight 方法

定義

回傳一個指定長度的新字串,該字串末尾會以空格或指定的 Unicode 字元填充。

多載

名稱 Description
PadRight(Int32)

回傳一個新字串,透過在右側用空格填充字元,使字元左對齊,並設定指定的總長度。

PadRight(Int32, Char)

回傳一個新字串,透過在右側用指定的 Unicode 字元填充字元,使字元左對齊,達到指定的總長度。

PadRight(Int32)

回傳一個新字串,透過在右側用空格填充字元,使字元左對齊,並設定指定的總長度。

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

所得字串中的字元數,等於原始字元數加上任何額外的填充字元。

傳回

一個與此實例等價的新字串,但左對齊,右側填充所需空格以形成長度。totalWidth 然而,如果 totalWidth 小於此實例的長度,該方法會回傳一個對現有實例的參考。 若 totalWidth 等於此實例的長度,該方法會回傳一個與此實例相同的新字串。

例外狀況

totalWidth 小於零。

範例

以下範例示範此 PadRight 方法。

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|".
let str = "BBQ and Slaw"

printf "|"
printf $"{str.PadRight 15}"
printfn "|"       // Displays "|BBQ and Slaw   |".

printf "|"
printf $"{str.PadRight 5}"
printfn "|"       // 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。

PadRight(Int32) 方法在回傳弦的末端填充。 這表示,當用於右至左語言時,它會填充字串的左部分。

Note

PadRight 方法在當前實例中填充空白字元,則不會改變當前實例的值。 取而代之的是,它回傳一個新字串,並以後方空白填充,使其總長度為 totalWidth 字元數。

另請參閱

適用於

PadRight(Int32, Char)

回傳一個新字串,透過在右側用指定的 Unicode 字元填充字元,使字元左對齊,達到指定的總長度。

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

所得字串中的字元數,等於原始字元數加上任何額外的填充字元。

paddingChar
Char

一個 Unicode 填充字元。

傳回

一個新字串,與此實例等價,但左對齊並在右側填充paddingChar所需字元以形成長度。totalWidth 然而,如果 totalWidth 小於此實例的長度,該方法會回傳一個對現有實例的參考。 若 totalWidth 等於此實例的長度,該方法會回傳一個與此實例相同的新字串。

例外狀況

totalWidth 小於零。

範例

以下範例示範此 PadRight 方法。

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

Console.WriteLine(str.PadRight(15, pad));    // Displays "forty-two......".
Console.WriteLine(str.PadRight(2,  pad));    // Displays "forty-two".
let str = "forty-two"
let pad = '.'

printfn $"{str.PadRight(15, pad)}"    // Displays "forty-two......".
printfn $"{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) 方法在回傳弦的末端填充。 這表示,當用於右至左語言時,它會填充字串的左部分。

Note

PadRight 方法在當前實例中填充空白字元,則不會改變當前實例的值。 它會回傳一個新字串,並以後面 paddingChar 字元填充,使其總長度為 totalWidth 字元數。

另請參閱

適用於