C# String PadLeft(Int32)

Description

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

Syntax

String.PadLeft(Int32) has the following syntax.


public string PadLeft(
  int totalWidth
)

Parameters

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

Returns

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

Example

The following example demonstrates the PadLeft method.


//  w ww.ja v a  2 s. co  m
using System;
public class MainClass{
  public static void Main(String[] argv){  
    string str = " java2s.com";
    Console.WriteLine(str.PadLeft(15));  
    Console.WriteLine(str.PadLeft(5));

  }
}

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