查看: 733|回复: 1

[ASP入门] ASP数组/集合遍历示范

[复制链接]
xuanxiao 发表于 2022-3-11 14:38:09 | 显示全部楼层 |阅读模式
在Asp中如何遍历数组?

  1. ' 创建一个固定数组
  2. dim companies(2)

  3. ' 填充数组
  4. set companies(0) = Company1
  5. set companies(1) = Company2
  6. set companies(2) = Company3

  7. ' 遍历数组方法1
  8. dim company
  9. for each company in companies
  10.     response.write company   '公司名
  11. next

  12. ' 遍历数组方法2
  13. dim i
  14. for i = 0 to ubound(companies)
  15.     response.write companies(i)  '公司名
  16. next
复制代码


在ASP中如何遍历集合?

  1. 方法1:
  2. dim cars(2),x
  3. cars(0)="Volvo"
  4. cars(1)="Saab"
  5. cars(2)="BMW"

  6. For Each x in cars
  7.   response.write(x & "<br ")
  8. Next

复制代码


在ASP中如何遍历字典:

  1. dim Cars
  2. set Cars = server.createObject("Scripting.Dictionary")

  3. ' 新增cars
  4. Cars.add "电动车", "蔚蓝"
  5. Cars.add "油电混合", "吉利"
  6. Cars.add "新能源", "比亚迪"

  7. ' 遍历方法1:
  8. dim key
  9. for each key in Cars.keys
  10.     response.write key & " = " & Cars(key)&VBCRLF
  11. next


  12. ' 遍历方法2:
  13. Dim objKeys, objItems, i
  14.   objKeys = Cars.Keys
  15.   objItems = Cars.Items
  16. For i = 0 To Cars.Count -1
  17. response.Write "Key=" & objKeys(i) &_
  18.                  " AND Value=" & objItems(i)&vbcrlf
  19. Next
复制代码
 楼主| xuanxiao 发表于 2023-4-21 12:29:40 | 显示全部楼层
Join 函数返回一个由数组中若干子字符串组成的字符串。

参数
描述
list必需。一维数组,其中包含需被连接的子字符串。
delimiter可选。用于在返回的字符串中分割子字符串的字符。默认是空格字符。


  1. days=Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
  2. document.write(Join(days) & "<br />")
  3. document.write(Join(days,",") & "<br />")
  4. document.write(Join(days," ### "))
复制代码


结果:
  1. Sun Mon Tue Wed Thu Fri Sat
  2. Sun,Mon,Tue,Wed,Thu,Fri,Sat
  3. Sun ### Mon ### Tue ### Wed ### Thu ### Fri ### Sat
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号