MattsBits
MattsBits

MattsBits

VBScript Function To Left Or Right Pad A String  

by Matt Hawkins, 29/10/2010
Categories : VBScript

Here is a VBScript function to left or right pad a string with a specified character.

It is useful for creating formatted columns of text in outputs from VBScript scripts.

Function PadString(pString,pLength,pChar,pSide)

  ' This function pads a string to a specified length
  
  ' pString - String to pad
  ' pLength - Required length
  ' pChar   - Single character to use for padding
  ' pSide   - Add padding on the "left" or "right"

  ' If the string is already longer than pLength it will
  ' be truncated.
  
  strString = pString
  
  ' Create padding of required length
  strPadding = String(pLength,pChar)  
  
  If lcase(pSide)="left" then
    strString = strPadding & strString
    strString = Right(strString,pLength)
  else
    strString = strString & strPadding
    strString = Left(strString,pLength)  
  End if
  
  PadString = strString  ' Return string  
   
End Function

Author : Matt Hawkins  Last Edit By : Matt Hawkins
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  Firefox - Take Back The Web  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v3.0 - Copyright 2009-2017