String.PadLeft 方法

定義

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

多載

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

產生的字串中的字元數,等於原始字元加上任何其他填補字元的數目。

傳回

String

與這個執行個體相等的新字串,但為靠右對齊,並在左側視需要填補若干空間來建立 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".
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 字元。

另請參閱

適用於

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 填補字元。

傳回

String

與這個執行個體相等的新字串,但為靠右對齊,並在左側視需要填補若干 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
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 字元。

另請參閱

適用於