Do while,直到变量为空/ null

时间:2017-04-13 13:03:32

标签: vbscript asp-classic

不确定这是否可行:

                myString = 313233

                counter = Len(myString)                     

                Do while counter > 0

                position = 1

                take = 2

                response.write (" counter:" & counter)

                first_two = Mid(myString, position, take)

                response.write (" Each loop:" & first_two)                                              

                position = position + 2

                counter = counter - 2

                Loop 

有人能告诉我哪里出错了吗?或者这可能通过不同的方法吗?

上面的代码运行一次并返回:31而不是31 32 33

感谢。

更新:很快就实现了我的错误,但现在位置不会增加以提升变量

1 个答案:

答案 0 :(得分:0)

您在position循环开始时将1变量设置为Do。每当调用Mid()时,它就会被传递position = 1,它将返回31

通过将其移到循环外部来解决此问题,以便position可以递增。

myString = 313233
counter = Len(myString)                     

'Set before start of loop
position = 1
take = 2

Do while counter > 0
  response.write (" counter:" & counter)
  first_two = Mid(myString, position, take)
  response.write (" Each loop:" & first_two)
  'Use take variable so increment is always the same.                                          
  position = position + take
  counter = counter - take
Loop

代码未经测试