Edit

Share via


String.PadLeft Method

Definition

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.

Overloads

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)

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)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

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.

C#
public string PadLeft(int totalWidth);

Parameters

totalWidth
Int32

The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.

Returns

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. However, if totalWidth is less than the length of this instance, the method returns a reference to the existing instance. If totalWidth is equal to the length of this instance, the method returns a new string that is identical to this instance.

Exceptions

totalWidth is less than zero.

Examples

The following example demonstrates the PadLeft method.

C#
string str = "BBQ and Slaw";
Console.WriteLine(str.PadLeft(15));  // Displays "   BBQ and Slaw".
Console.WriteLine(str.PadLeft(5));   // Displays "BBQ and Slaw".

Remarks

A Unicode space is defined as hexadecimal 0x0020.

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.

Note

If the PadLeft method pads the current instance with white-space characters, this method does not modify the value of the current instance. Instead, it returns a new string that is padded with leading white space so that its total length is totalWidth characters.

See also

Applies to

.NET 9 and other versions
Product Versions
.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

PadLeft(Int32, Char)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

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.

C#
public string PadLeft(int totalWidth, char paddingChar);

Parameters

totalWidth
Int32

The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.

paddingChar
Char

A Unicode padding character.

Returns

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. However, if totalWidth is less than the length of this instance, the method returns a reference to the existing instance. If totalWidth is equal to the length of this instance, the method returns a new string that is identical to this instance.

Exceptions

totalWidth is less than zero.

Examples

The following example demonstrates the PadLeft method.

C#
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

Remarks

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.

Note

If the PadLeft method pads the current instance with white-space characters, this method does not modify the value of the current instance. Instead, it returns a new string that is padded with leading paddingChar characters so that its total length is totalWidth characters.

See also

Applies to

.NET 9 and other versions
Product Versions
.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