String.PadLeft 方法

定義

傳回指定之長度的新字串,其中目前字串的開頭將以空白和或指定的 Unicode 字元填補。Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character.

多載

PadLeft(Int32, Char)

傳回新字串,此字串會以指定的 Unicode 字元填補左側至指定的總長度,靠右對齊這個執行個體中的字元。Returns a new string that right-aligns the characters in this instance by padding them on the left with a specified Unicode character, for a specified total length.

PadLeft(Int32)

傳回新字串,此字串會以空格填補左側至指定的總長度,靠右對齊這個執行個體中的字元。Returns a new string that right-aligns the characters in this instance by padding them with spaces on the left, for a specified total length.

PadLeft(Int32, Char)

傳回新字串,此字串會以指定的 Unicode 字元填補左側至指定的總長度,靠右對齊這個執行個體中的字元。Returns a new string that right-aligns the characters in this instance by padding them on the left with a specified Unicode character, for a specified total length.

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

產生的字串中的字元數,等於原始字元加上任何其他填補字元的數目。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 字元來建立 totalWidth 的長度。A new string that is equivalent to this instance, but right-aligned and padded on the left 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.

範例

下列範例示範 PadLeft 方法。The following example demonstrates the PadLeft method.

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) 方法會填補傳回字串的開頭。The PadLeft(Int32, Char) method pads the beginning of the returned string. 這表示,當與由右至左的語言搭配使用時,它會填補字串的正確部分。This means that, when used with right-to-left languages, it pads the right portion of the string.

注意

如果 PadLeft 方法使用空白字元來填補目前的實例,這個方法就不會修改目前實例的值。If the PadLeft 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 leading paddingChar characters so that its total length is totalWidth characters.

另請參閱

PadLeft(Int32)

傳回新字串,此字串會以空格填補左側至指定的總長度,靠右對齊這個執行個體中的字元。Returns a new string that right-aligns the characters in this instance by padding them with spaces on the left, for a specified total length.

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

產生的字串中的字元數,等於原始字元加上任何其他填補字元的數目。The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.

傳回

與這個執行個體相等的新字串,但為靠右對齊,並在左側視需要填補若干空間來建立 totalWidth 的長度。A new string that is equivalent to this instance, but right-aligned and padded on the left 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.

範例

下列範例示範 PadLeft 方法。The following example demonstrates the PadLeft method.

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。A Unicode space is defined as hexadecimal 0x0020.

PadLeft(Int32) 方法會填補傳回字串的開頭。The PadLeft(Int32) method pads the beginning of the returned string. 這表示,當與由右至左的語言搭配使用時,它會填補字串的正確部分。This means that, when used with right-to-left languages, it pads the right portion of the string.

注意

如果 PadLeft 方法使用空白字元來填補目前的實例,這個方法就不會修改目前實例的值。If the PadLeft 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 leading white space so that its total length is totalWidth characters.

另請參閱

適用於