Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleLeft or right pad a string with a character
Keywordspad, fill
CategoriesStrings
 
To pad on the left, use the String function to make a string of the pad character that is as long as you want the result to be. Add this padding to the left of the original value. Then use Right$ to trim the result to the desired length.

To pad on the right, add the pad string to the right of the original value and use Left$ to trim the result.

 
Private Sub cmdPad_Click()
Dim value As String
Dim pad_text As String
Dim length As Integer

    value = txtString.Text
    pad_text = txtPad.Text
    length = CInt(txtLength.Text)

    If optSide(0).value Then
        ' Pad on left.
        txtResult.Text = Right$( _
            String(length, pad_text) & value, length)
    Else
        ' Pad on right.
        txtResult.Text = Left$( _
            value & String(length, pad_text), length)
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated