C# String PadLeft(Int32, Char)

Description

String 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.

Syntax

String.PadLeft(Int32, Char) has the following syntax.


public string PadLeft(
  int totalWidth,
  char paddingChar
)

Parameters

String.PadLeft(Int32, Char) has the following parameters.

  • totalWidth - The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.
  • paddingChar - A Unicode padding character.

Returns

String.PadLeft(Int32, Char) method 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.

Example

The following example demonstrates the PadLeft method.


/*w w w  . j a  v a  2  s .c om*/
using System;

class Sample
{
   public static void Main()
   { 
       string str = "java2s.com";
       char pad = '.';
    
       Console.WriteLine(str.PadLeft(15, pad));  
       Console.WriteLine(str.PadLeft(2, pad));  
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version