String Padding in VBScript
31 Dec 2010I’ve frequently found myself needing to pad text strings with other text when putting together VBScripts. Typically this is with regards to time related outputs (pre-fixing with zeroes), or numeric outputs for things like filenames (again prefixing with zeroes). Occasionally I need to do more advanced padding and so I created a set of code to add to my code library to cover a wider set of padding circumstances including not only pre-fixing, but also post-fixing variable length strings … and even bo th together.
The code below provides three functions that are the simplest way of making use of the underlying functions. These are as follows:
PostPadString()
- adds characters to the end of the original string to create a new string of the specified length.PrePadString()
- adds characters to the start of the original string to create a new string of the specified length.CentraliseString()
- adds characters as evenly as possible to the start and end of the original string to create a new string of the specified length.
After the main code block I’ve included some simple message box calls to
demonstrate how these are used, but there is one thing to note first.
Within the more generic PadString() function are two constants -
PRIORITY_PREFIX
and TRUNCATE_ENABLED
.
PRIORITY_PREFIX
is used by the script to determine which side of the
string should be weighted when trying to pad both ends. For example I
have an original string of characters “abc” which I want to extend to be
six characters long and centralise using hyphens. So should the script
output “–abc-“ or “-abc–”? Well, that’s up to you. By setting the
PRIORITY_PREFIX
constant to TRUE you would get the first result where
the final hyphen addition is made to the start of the centralised
string. Setting it to FALSE would consequently provide the alternative
result.
TRUNCATE_ENABLED
is used by the script to determine what to do if the
original string supplied is too long. Do you want to return the original
string intact or is it more important to return a string of the right
length and just return the first characters of the string up to the
length of the string requested. When TRUNCATE_ENABLED
is set to TRUE a
truncated string will be returned.
So here’s the script. It has a few comments in it to help describe what’s going on.
Option Explicit
Function PostPadString(p_strOriginal, p_strPadWith, p_strFinalLength)
PostPadString = PadString(p_strOriginal, p_strPadWith, p_strFinalLength, False, True)
End Function
Function PrePadString(p_strOriginal, p_strPadWith, p_strFinalLength)
PrePadString = PadString(p_strOriginal, p_strPadWith, p_strFinalLength, True, False)
End Function
Function CentraliseString(p_strOriginal, p_strPadWith, p_strFinalLength)
CentraliseString = PadString(p_strOriginal, p_strPadWith, p_strFinalLength, True, True)
End Function
Function PadString(p_strOriginal, p_strPadWith, p_strFinalLength, p_boolPadBefore, p_boolPadAfter)
'Function defaults
Const PRIORITY_PREFIX = True 'If we're prefixing and post fixing, does prefixing take priority?
Const TRUNCATE_ENABLED = True 'If the original string is too long, truncate it.
Dim intPrefixChars, intPostfixChars
'First check if the string is too long.
If len(p_strOriginal) > p_strFinalLength Then
If TRUNCATE_ENABLED Then
PadString = left(p_strOriginal, p_strFinalLength)
Else
PadString = p_strOriginal
End If
Else
'MsgBox p_boolPadBefore, 0, "p_boolPadBefore"
'MsgBox p_boolPadAfter, 0, "p_boolPadAfter"
'Calculate the number of characters to prefix and postfix
If p_boolPadBefore = p_boolPadAfter then
'Centralise
If p_boolPadBefore then
If IsEven(p_strFinalLength - len(p_strOriginal)) then
'Even split
intPrefixChars = (p_strFinalLength - len(p_strOriginal))/2
intPostfixChars = intPrefixChars
Else
'Uneven split
intPrefixChars = (p_strFinalLength - len(p_strOriginal) - 1)/2
intPostfixChars = intPrefixChars
If PRIORITY_PREFIX then
intPrefixChars = intPrefixChars + 1
Else
intPostfixChars = intPostfixChars + 1
End If
End If
Else
'No padding is to be done so set both to zero
intPrefixChars = 0
intPostfixChars = 0
End If
Else
'Only one is true so it is nice and simple, we just want to either prefix or postfix all characters
If p_boolPadBefore then
intPrefixChars = p_strFinalLength - len(p_strOriginal)
intPostfixChars = 0
Else
intPrefixChars = 0
intPostfixChars = p_strFinalLength - len(p_strOriginal)
End If
End If
'Do the padding
PadString = GenerateString(p_strPadWith, intPrefixChars) & p_strOriginal & GenerateString(p_strPadWith, intPostfixChars)
End If
End Function
Function GenerateString(p_strCharacters, p_intLength)
'Initialise
GenerateString = ""
'Keep appending the character string until we have equalled or exceeded the required length
While len(GenerateString) < p_intLength
GenerateString = GenerateString & p_strCharacters
Wend
'If we've exceeded the length, truncate it
GenerateString = left(GenerateString, p_intLength)
End Function
Function IsEven(p_intNumber)
If p_intNumber Mod 2 = 0 Then
IsEven = True
Else
IsEven = False
End If
End Function
The code below provides an example of each of the three functions with the same original string, a multi-character padding string and equal size requirement. Each string is also delimited by asterisks to help visualise what is happening.
MsgBox "*" & PostPadString("Original", "$%^", 15) & "*"
MsgBox "*" & PrePadString("Original", "$%^", 15) & "*"
MsgBox "*" & CentraliseString("Original", "$%^", 15) & "*"
This might be overkill for many scenarios where you may just need to round out a number to two digits or the like, but having a more sophisticated script in hand may just help with those more complicated string padding requirements that crop up from time to time.