使用经典 asp 将字符串分成块

splitting a string in chunks using classic asp

我得到了一个逗号分隔值列表 (a,b,c,d,e,f,g,h,....) 我希望将它们分成 5 个块,例如 (a,b,c,d,e) (f,g,h,i,j).... 有人可以帮我处理经典 asp 中的代码吗?

arr = Split(messto, ",") ' convert to array
totalemails = UBound(arr) ' total number of emails

if totalemails mod 5 = 0 then
    totalloops = int(totalemails/5) 
    else
    totalloops = int(totalemails/5) + 1
end if

x = 0 
y = 0
b = 0
for x = 0 to totalloops  


    for counter = (5* x)  to ((b+5)-1)
        if Trim(arr(counter)) <> "" and isnull(trim(arr(counter))) = false then 

        response.Write(Trim(arr(counter)))
        response.Write(counter & "<br>")
        mymssto =  mymssto & Trim(arr(counter)) & ","
        response.Write(mymssto)

        end if  

    next

您想使用 Mod() 来执行此操作,它非常强大且未得到充分利用。

这是一个基于问题中代码的简单示例;

<%
Dim mumberToGroupBy: numberToGroupBy = 5
Dim index, counter, arr, messto

messto = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q"
arr = Split(messto, ",") ' convert to array

For counter = 0 To UBound(arr)
  'Can't divide by 0 so we need to make sure our counter is 1 based.
  index = counter + 1
  Call Response.Write(Trim(arr(counter)))
  'Do we have any remainder in the current grouping?
  If index Mod numberToGroupBy = 0 Then Response.Write("<br>")
Next
%>

输出:

abcde
fghij
klmno
pq

有用的链接

  • A: Change response to only respond one set of values(详细说明Mod()的用法)