如何在VBscript中创建带填充零的标记

如何在VBscript中创建带填充零的标记,vbscript,Vbscript,我尝试使用以下代码用标记值填充列表框。在for循环中,我创建了要插入列表框的标记。我的问题是如何用零填充标签名编号?例如ValveName001-ValveName120 Dim i, listbox1, listbox2, tag Set listbox1 = ScreenItems("ListBoxValveName") For i = 1 To 120 tag = "ValveName" & "##" & i & ""

我尝试使用以下代码用标记值填充列表框。在for循环中,我创建了要插入列表框的标记。我的问题是如何用零填充标签名编号?例如ValveName001-ValveName120

    Dim i, listbox1, listbox2, tag
    Set listbox1 = ScreenItems("ListBoxValveName")

    For i = 1 To 120
         tag = "ValveName" & "##" & i & ""
         listbox1.SelectedIndex = i
         Set listbox1.SelectedText = SmartTags.Item(tag)
    Next

包括填充并仅获取所需字符

,或投资于更普遍适用(更安全)的功能。要开始,请执行以下操作:

Option Explicit

' pad (stringable) value v on the left to width w using fill character f
Function padLeft(v, w, f)
  If Len(v) < w Then
     padLeft = Right(String(w, f) & v, w)
  Else
     padLeft = v
  End If
End Function

Dim v : v = "1"
Dim w : w = 3
Dim f : f = "0"
WScript.Echo v, w, f, padLeft(v, w, f)
改进的功能(从@Bond的评论中盗取):

功能焊盘左侧(v、w、f)
尺寸l:l=长度(v)
如果l
padLeft=String(w-Len(v),f)&v
,它将
Right()
调用替换为
Len()
调用。
Option Explicit

' pad (stringable) value v on the left to width w using fill character f
Function padLeft(v, w, f)
  If Len(v) < w Then
     padLeft = Right(String(w, f) & v, w)
  Else
     padLeft = v
  End If
End Function

Dim v : v = "1"
Dim w : w = 3
Dim f : f = "0"
WScript.Echo v, w, f, padLeft(v, w, f)
cscript 26163030.vbs
1 3 0 001
Function padLeft(v, w, f)
  Dim l : l = Len(v)
  If l < w Then
     padLeft = String(w - l, f) & v
  Else
     padLeft = v
  End If
End Function