CorString: PadLeft

PadLeft

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.



 Public Function PadLeft(
	  ByRef s As String,
	  ByVal TotalWidth As Long,
	  Optional ByRef PaddingChar As Variant = 32 ) As String

Parameters

s
[ByRef] String. The string to be padded.
TotalWidth
[ByVal] Long. The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.
PaddingChar
[ByRef] Optional. Variant. A Unicode padding character. AscW and Chrw$ values are accepted.  

Default: 32

Return Values

String -  A new string that is equivalent to s, 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 s, the method returns s. If TotalWidth is equal to the length of s, the method returns a new string that is identical to s.

Remarks

The PadLeft(String,Int32, Char) method pads the beginning of the returned string.

PaddingChar can accept either AscW or ChrW$ values.

Note This method does not modify the s parameter. Instead, it returns a new string that is padded with leading PaddingChar characters so that its total length is TotalWidth characters.

Exceptions

ExceptionCondition
ArgumentOutOfRangeException TotalWidth is less than zero.

Examples

The following example demonstrates the PadLeft method.

Public Sub Main()
    Const Str As String = "Forty-Two"
    Const StrPad As String = "."
    Const CharPad As Integer = 46
    
    Debug.Print CorString.PadLeft(Str, 15, StrPad)
    Debug.Print CorString.PadLeft(Str, 15, CharPad)
    Debug.Print CorString.PadLeft(Str, 2, StrPad)
End Sub

' The previous code outputs the following.
'
'    ......Forty-Two
'    ......Forty-Two
'    Forty-Two

See Also

Project CorLib Overview

Class CorString Overview