Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lpad with zero's in vbscript

I'm trying to pad a string with 0's to the left.The length of the output string should be 7. Here's my code :

inputstr = "38"
in = string(7 - Len(inputStr),0) & inputStr
msgbox in

I'm getting error Expected Statement Please help me Thank You

like image 916
Yousuf Khan Avatar asked Aug 09 '13 16:08

Yousuf Khan


Video Answer


2 Answers

The following code will run 5% faster:

inputStr = "38"
result = Right("0000000" & inputStr, 7)

msgbox result
like image 125
Andrej Kirejeŭ Avatar answered Oct 12 '22 22:10

Andrej Kirejeŭ


This function will left-pad an input value to the given number of characters using the given padding character without truncating the input value:

Function LPad(s, l, c)
  Dim n : n = 0
  If l > Len(s) Then n = l - Len(s)
  LPad = String(n, c) & s
End Function

Output:

>>> WScript.Echo LPad(12345, 7, "0")
0012345
>>> WScript.Echo LPad(12345, 3, "0")
12345
like image 30
Ansgar Wiechers Avatar answered Oct 12 '22 22:10

Ansgar Wiechers