Use one of the following System.String methods to create a new string that consists of an original string that is padded with leading or trailing characters to a specified total length. The padding character can be a space or a specified character. The resulting string appears to be either right-aligned or left-aligned. If the original string's length is already equal to or greater than the desired total length, the padding methods return the original string unchanged; for more information, see the Returns sections of the two overloads of the System.String.PadLeft and System.String.PadRight methods.
| Method name | Use |
|---|---|
| System.String.PadLeft | Pads a string with leading characters to a specified total length. |
| System.String.PadRight | Pads a string with trailing characters to a specified total length. |
PadLeft
The System.String.PadLeft method creates a new string by concatenating enough leading pad characters to an original string to achieve a specified total length. The System.String.PadLeft%28System.Int32%29 method uses white space as the padding character and the System.String.PadLeft%28System.Int32%2CSystem.Char%29 method enables you to specify your own padding character.
The following code example uses the System.String.PadLeft method to create a new string that is twenty characters long. The example displays "--------Hello World!" to the console.
sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.string.basicops/cs/padding.cs3
// <snippet2>
using System;
class Example
{
public static void Main()
{
PadLeft();
PadRight();
}
public static void PadLeft()
{
// <snippet3>
string MyString = "Hello World!";
Console.WriteLine(MyString.PadLeft(20, '-'));
// </snippet3>
}
public static void PadRight()
{
// <snippet4>
string MyString = "Hello World!";
Console.WriteLine(MyString.PadRight(20, '-'));
// </snippet4>
}
}
// </snippet2>sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.string.basicops/vb/padding.vb3
Option Strict On
' <snippet2>
Class Example
Public Shared Sub Main()
PadLeft()
PadRight()
End Sub
Public Shared Sub PadLeft()
' <snippet3>
Dim MyString As String = "Hello World!"
Console.WriteLine(MyString.PadLeft(20, "-"c))
' </snippet3>
End Sub
Public Shared Sub PadRight()
' <snippet4>
Dim MyString As String = "Hello World!"
Console.WriteLine(MyString.PadRight(20, "-"c))
' </snippet4>
End Sub
End Class
' </snippet2>PadRight
The System.String.PadRight method creates a new string by concatenating enough trailing pad characters to an original string to achieve a specified total length. The System.String.PadRight%28System.Int32%29 method uses white space as the padding character and the System.String.PadRight%28System.Int32%2CSystem.Char%29 method enables you to specify your own padding character.
The following code example uses the System.String.PadRight method to create a new string that is twenty characters long. The example displays "Hello World!--------" to the console.
sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.string.basicops/cs/padding.cs4
// <snippet2>
using System;
class Example
{
public static void Main()
{
PadLeft();
PadRight();
}
public static void PadLeft()
{
// <snippet3>
string MyString = "Hello World!";
Console.WriteLine(MyString.PadLeft(20, '-'));
// </snippet3>
}
public static void PadRight()
{
// <snippet4>
string MyString = "Hello World!";
Console.WriteLine(MyString.PadRight(20, '-'));
// </snippet4>
}
}
// </snippet2>sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.string.basicops/vb/padding.vb4
Option Strict On
' <snippet2>
Class Example
Public Shared Sub Main()
PadLeft()
PadRight()
End Sub
Public Shared Sub PadLeft()
' <snippet3>
Dim MyString As String = "Hello World!"
Console.WriteLine(MyString.PadLeft(20, "-"c))
' </snippet3>
End Sub
Public Shared Sub PadRight()
' <snippet4>
Dim MyString As String = "Hello World!"
Console.WriteLine(MyString.PadRight(20, "-"c))
' </snippet4>
End Sub
End Class
' </snippet2>