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 字符,使长度达到 totalWidthA 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.

返回

与此实例等效的一个新字符串,但该字符串为左对齐,因此,在右侧填充所需任意数量的空格,使长度达到 totalWidthA 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.

另请参阅

适用于