String.PadRight 方法

定義

傳回指定之長度的新字串,其中目前字串的結尾將以空白和或指定的 Unicode 字元填補。

多載

PadRight(Int32)

傳回新字串,此字串會以空格填補右側至指定的總長度,靠左對齊這個字串中的字元。

PadRight(Int32, Char)

傳回新字串,此字串會以指定的 Unicode 字元填補右側至指定的總長度,靠左對齊這個字串中的字元。

PadRight(Int32)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

傳回新字串,此字串會以空格填補右側至指定的總長度,靠左對齊這個字串中的字元。

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 = "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|".
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) 會填補傳回字串的結尾。 這表示,搭配由右至左語言使用時,它會填補字串的左部分。

注意

PadRight如果方法以空白字元填補目前的實例,這個方法不會修改目前實例的值。 相反地,它會傳回以尾端空白字元填補的新字串,使其總長度為 totalWidth 字元。

另請參閱

適用於

PadRight(Int32, Char)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

傳回新字串,此字串會以指定的 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";
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".
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) 會填補傳回字串的結尾。 這表示,搭配由右至左語言使用時,它會填補字串的左部分。

注意

PadRight如果方法以空白字元填補目前的實例,這個方法不會修改目前實例的值。 相反地,它會傳回以尾 paddingChar 端字元填補的新字串,使其總長度是 totalWidth 字元。

另請參閱

適用於