Search Tools Links Login

PadString Any Side

Posted: 2002-06-01
By: ArchiveBot
Viewed: 67

Filed Under:

VB6 Code Cache

No attachments for this post

Pads a string with any character you like. I usually use it to pad numbers with leading zeros. But you can use it for other things.

Original Author: VisualProgman


Input ltring, length of return string, pad character, and side to pad


a string

Side Effects

No matter howmany characters in the pstrChar your pad character is the first character of pstrChar.

API Declarations

Public Enum enPadString
End Enum


Public Function PadString(pstrInput As String, _
  pintWidth As Integer, _
  pstrChar As String, _
  Optional penSidetoPad As enPadString = pdLeft) As String
  'PadString("12345", 10, "0") = "0000012345"
  'PadString("12345", 10, "0", pdRight)) = "1234500000"
  'Declare Variables
  Dim strTemp As String
  'End Declares
  'Creates a string to the length of
  'pintWidth of the first character
  'of pstrChar.
  strTemp = String$(pintWidth, pstrChar)
  'Check to see what side to pad?
  If penSidetoPad = pdRight Then
    PadString = Left$(pstrInput & strTemp, pintWidth)
    PadString = Right$(strTemp & pstrInput, pintWidth)
  End If
End Function 'PadString

Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.