C# String PadRight(Int32, Char)

Description

String PadRight(Int32, Char) returns a new string that left-aligns the characters in this string by padding them on the right with a specified Unicode character, for a specified total length.

Syntax

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


public string PadRight(
  int totalWidth,
  char paddingChar
)

Parameters

String.PadRight(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.PadRight(Int32, Char) method returns A new string that is equivalent to this instance, but left-aligned and padded on the right 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 PadRight method.


//from  w  w w. ja va 2  s.c  o  m
using System;
public class MainClass{
  public static void Main(String[] argv){  
    string str = "forty-two";
    char pad = '.';
    
    Console.WriteLine(str.PadRight(15, pad));
    Console.WriteLine(str.PadRight(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