VBScript str_pad function

A VBScript equivalent of PHP’s str_pad


Const STR_PAD_LEFT  = 0
Const STR_PAD_RIGHT = 1
Const STR_PAD_BOTH  = 2
Function str_pad(byVal input, pad_length, pad_string, pad_type)

    Dim half : half = "
    Dim pad_to_go

    If pad_type <> STR_PAD_LEFT and pad_type <> STR_PAD_RIGHT and pad_type <> STR_PAD_BOTH Then
        pad_type = STR_PAD_RIGHT
    End If

    If len(pad_string) = 0 Then pad_string = " "

    pad_to_go = pad_length - len( input )
    If pad_to_go > 0 Then
        If pad_type = STR_PAD_LEFT Then
            input = str_pad_helper(pad_string, pad_to_go) & input
        ElseIf pad_type = STR_PAD_RIGHT Then
            input = input & str_pad_helper(pad_string, pad_to_go)
        ElseIf pad_type = STR_PAD_BOTH Then
            half = str_pad_helper(pad_string,intval(pad_to_go/2))
            input = half & input & half
            input = Left(input,pad_length)
        End If
    End if

    str_pad = input

End Function

Function str_pad_helper(s, intlen)

    Dim collect : collect = "
    Dim i

    Do Until len( collect ) > intlen
        collect = collect & s
    Loop

    collect = Left(collect,intlen)

    str_pad_helper = collect

End Function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the strings extension