String.PadLeft 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳一個指定長度的新字串,該字串的開頭會以空格或指定的 Unicode 字元填充。
多載
| 名稱 | Description |
|---|---|
| PadLeft(Int32) |
回傳一個新字串,透過在左側用空格填充字元,將字元右對齊,並設定指定的總長度。 |
| PadLeft(Int32, Char) |
回傳一個新字串,透過在左側用指定的 Unicode 字元填充字元,使字元右對齊,達到指定的總長度。 |
PadLeft(Int32)
回傳一個新字串,透過在左側用空格填充字元,將字元右對齊,並設定指定的總長度。
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".
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) 方法填充回傳弦的起始。 這表示,當用於從右到左的語言時,它會填充字串的右側部分。
Note
若 PadLeft 方法在當前實例中填充空白字元,則不會改變當前實例的值。 取而代之的是回傳一個新字串,並以前導空白填充,使其總長度為 totalWidth 字元數。
另請參閱
適用於
PadLeft(Int32, Char)
回傳一個新字串,透過在左側用指定的 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 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) 方法填充回傳弦的起始。 這表示,當用於從右到左的語言時,它會填充字串的右側部分。
Note
若 PadLeft 方法在當前實例中填充空白字元,則不會改變當前實例的值。 取而代之的是,它回傳一個新字串,並填充了前導 paddingChar 字元,使其總長度為 totalWidth 字元數。