String.PadLeft 方法
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个指定长度的新字符串,其中在当前字符串的开头填充空格或指定的 Unicode 字符。
PadLeft(Int32, Char) |
返回一个新字符串,该字符串通过在此实例中的字符左侧填充指定的 Unicode 字符来达到指定的总长度,从而使这些字符右对齐。 |
PadLeft(Int32) |
返回一个新字符串,该字符串通过在此实例中的字符左侧填充空格来达到指定的总长度,从而实现右对齐。 |
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
返回一个新字符串,该字符串通过在此实例中的字符左侧填充指定的 Unicode 字符来达到指定的总长度,从而使这些字符右对齐。
public:
System::String ^ PadLeft(int totalWidth, char paddingChar);
public string PadLeft (int totalWidth, char paddingChar);
member this.PadLeft : int * char -> string
Public Function PadLeft (totalWidth As Integer, paddingChar As Char) As String
参数
- totalWidth
- Int32
结果字符串中的字符数,等于原始字符数加上任何其他填充字符。
- paddingChar
- Char
Unicode 填充字符。
返回
与此实例等效的一个新字符串,但该字符串为右对齐,因此,在左侧填充所需任意数量的 paddingChar
字符,使长度达到 totalWidth
。 但是,如果 totalWidth
小于此实例的长度,则此方法返回对现有实例的引用。 如果 totalWidth
等于此实例的长度,则此方法返回与此实例相同的新字符串。
例外
totalWidth
小于零。
示例
下面的示例演示 PadLeft 方法。
using namespace System;
void main()
{
String^ str = "forty-two";
Console::WriteLine( str->PadLeft( 15, L'.' ) );
Console::WriteLine( str->PadLeft( 2, L'.' ) );
}
// The example displays the following output:
// ......forty-two
// forty-two
using System;
class Sample
{
public static void Main()
{
string str = "forty-two";
char pad = '.';
Console.WriteLine(str.PadLeft(15, pad));
Console.WriteLine(str.PadLeft(2, pad));
}
}
// The example displays the following output:
// ......forty-two
// forty-two
let str = "forty-two"
let pad = '.'
printfn $"{str.PadLeft(15, pad)}"
printfn $"{str.PadLeft(2, pad)}"
// The example displays the following output:
// ......forty-two
// forty-two
Public Class Example
Public Shared Sub Main()
Dim str As String
Dim pad As Char
str = "forty-two"
pad = "."c
Console.WriteLine(str.PadLeft(15, pad))
Console.WriteLine(str.PadLeft(2, pad))
End Sub
End Class
' The example displays the following output:
' ......forty-two
' forty-two
注解
方法 PadLeft(Int32, Char) 填充返回的字符串的开头。 这意味着,当与从右到左的语言一起使用时,它会填充字符串的右侧部分。
备注
PadLeft如果 方法用空格字符填充当前实例,则此方法不会修改当前实例的值。 相反,它返回一个新字符串,该字符串填充前导 paddingChar
字符,使其总长度为 totalWidth
字符。
另请参阅
适用于
.NET 9 和其他版本
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
返回一个新字符串,该字符串通过在此实例中的字符左侧填充空格来达到指定的总长度,从而实现右对齐。
public:
System::String ^ PadLeft(int totalWidth);
public string PadLeft (int totalWidth);
member this.PadLeft : int -> string
Public Function PadLeft (totalWidth As Integer) As String
参数
- totalWidth
- Int32
结果字符串中的字符数,等于原始字符数加上任何其他填充字符。
返回
与此实例等效的一个新字符串,但该字符串为右对齐,因此,在左侧填充所需任意数量的空格,使长度达到 totalWidth
。 但是,如果 totalWidth
小于此实例的长度,则此方法返回对现有实例的引用。 如果 totalWidth
等于此实例的长度,则此方法返回与此实例相同的新字符串。
例外
totalWidth
小于零。
示例
下面的示例演示 PadLeft 方法。
String^ str = "BBQ and Slaw";
Console::WriteLine( str->PadLeft( 15 ) ); // Displays " BBQ and Slaw".
Console::WriteLine( str->PadLeft( 5 ) ); // Displays "BBQ and Slaw".
string str = "BBQ and Slaw";
Console.WriteLine(str.PadLeft(15)); // Displays " BBQ and Slaw".
Console.WriteLine(str.PadLeft(5)); // Displays "BBQ and Slaw".
let str = "BBQ and Slaw"
printfn $"{str.PadLeft 15}" // Displays " BBQ and Slaw".
printfn $"{str.PadLeft 5}" // Displays "BBQ and Slaw".
Dim str As String
str = "BBQ and Slaw"
Console.WriteLine(str.PadLeft(15)) ' Displays " BBQ and Slaw".
Console.WriteLine(str.PadLeft(5)) ' Displays "BBQ and Slaw".
注解
Unicode 空间定义为十六进制0x0020。
方法 PadLeft(Int32) 填充返回的字符串的开头。 这意味着,当与从右到左的语言一起使用时,它会填充字符串的右侧部分。
备注
PadLeft如果 方法用空格字符填充当前实例,则此方法不会修改当前实例的值。 相反,它返回一个新字符串,该字符串填充有前导空格,使其总长度为 totalWidth
字符。
另请参阅
适用于
.NET 9 和其他版本
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |