VBS:计算数组中的项目数

Gur*_*bel 4 arrays vbscript

我怎么能简单地调查给定数组中的项目数。我用了这个代码,但是有点费力

myArr=Array("frog", "cat", "bat", "rat", "horse")
 for i=0 to UBound(myArr)
 ' Msgbox i +1 & ".item: " & myArr(i) 
 next
Msgbox i & " items in this array:" & vbcrlf & Join(myArr)
Run Code Online (Sandbox Code Playgroud)

谢谢

JaG*_*oTu 7

嗯……你的代码中有它。UBound(Array)返回上限,即最高的现有项目。UBound(myArr) + 1是它的长度,因为索引是从零开始的。

  • 如果我回答了您的问题,请将其标记为解决方案!谢谢 (3认同)

sha*_*esh 1

Msgbox (ubound(myArr) + 1) & " items in this array:" & vbcrlf & Join(myArr)

编辑:当您已经使用时ubound,为什么需要循环和变量i来计数。