String.PadLeft メソッド

定義

現在の文字列の先頭にスペースまたは指定した Unicode 文字を埋め込む、指定した長さの新しい文字列を返します。

オーバーロード

名前 説明
PadLeft(Int32)

指定した合計長の左にスペースを埋め込むことで、このインスタンス内の文字を右揃えする新しい文字列を返します。

PadLeft(Int32, Char)

指定した長さの Unicode 文字を左に埋め込むことで、このインスタンス内の文字を右揃えにする新しい文字列を返します。

PadLeft(Int32)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
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 が 0 未満です。

次の例では、 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 スペースは、16 進数の0x0020として定義されます。

PadLeft(Int32)メソッドは、返された文字列の先頭を埋め込みます。 つまり、右から左の言語で使用すると、文字列の右側の部分が埋め込まれます。

Note

PadLeft メソッドが現在のインスタンスに空白文字を埋め込む場合、このメソッドは現在のインスタンスの値を変更しません。 代わりに、先頭に空白が埋め込まれた新しい文字列が返され、その合計の長さが totalWidth 文字になります。

こちらもご覧ください

適用対象

PadLeft(Int32, Char)

ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
String.Manipulation.cs
ソース:
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 が 0 未満です。

次の例では、 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 文字になります。

こちらもご覧ください

適用対象