String.PadRight 方法

定义

返回指定长度的新字符串,其中当前字符串的末尾用空格或指定的 Unicode 字符填充。

重载

名称 说明
PadRight(Int32)

返回一个新字符串,该字符串通过用右侧的空格填充字符,使该字符串中的字符保持左对齐,以指定的总长度。

PadRight(Int32, Char)

返回一个新字符串,该字符串通过用指定的 Unicode 字符在右侧填充字符以指定总长度来使字符串中的字符左对齐。

PadRight(Int32)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
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;
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
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";
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 字符。

另请参阅

适用于