Vbscript:将文本字符串转换为小块并将其放入数组中

时间:2011-08-31 13:42:57

标签: arrays string text vbscript split

我需要将一个长文本字符串分成大约每500个字符(不是一个特殊字符)的小块,形成一个所有句子的数组,然后将它们放在一起由特定字符(例如/ /)分隔。如下:

“这段文字非常大。”

所以,我得到了:

arrTxt(0) = "This is"
arrTxt(1) = "a very"
arrTxt(2) = "very large text"
...

最后:

response.write arrTxt(0) & "//" & arrTxt(1) & "//" & arrTxt(2)...

由于我对经典asp的了解有限,我得到的最接近的结果如下:

length = 200
strText = "This text is a very very large."
lines = ((Len (input) / length) - 1)
For i = 0 To (Len (lines) - 1)
txt = Left (input, (i * length)) & "/ /"
response.write txt
Next

然而,这会返回一个重复且重叠的文本字符串:“这是/ /这是一个/ /这是一个文本//...

对vbscript有什么想法吗?谢谢!

2 个答案:

答案 0 :(得分:2)

不使用数组,您可以随时构建字符串

Const LIMIT = 500
Const DELIMITER = "//"
' your input string - String() creates a new string by repeating the second parameter by the given
' number of times
dim INSTRING: INSTRING = String(500, "a") & String(500, "b") & String(500, "c")


dim current: current = Empty
dim rmainder: rmainder = INSTRING 
dim output: output = Empty
' loop while there's still a remaining string
do while len(rmainder) <> 0
    ' get the next 500 characters
    current = left(rmainder, LIMIT)
    ' remove this 500 characters from the remainder, creating a new remainder
    rmainder = right(rmainder, len(rmainder) - len(current))
    ' build the output string
    output  = output  & current & DELIMITER
loop
' remove the lastmost delimiter
output = left(output, len(output) - len(DELIMITER))
' output to page
Response.Write output

如果你真的需要一个数组,那么你可以split(output, DELIMITER)

答案 1 :(得分:1)

这是一个尝试:

Dim strText as String
Dim strTemp as String
Dim arrText()
Dim iSize as Integer
Dim i as Integer

strText = "This text is a very very large."
iSize = Len(stText) / 500
ReDim arrText(iSize)
strTemp = strText

For i from 0 to iSize - 1
  arrText(i) = Left(strTemp, 500)
  strTemp = Mid(strTemp, 501)
Next i

WScript.Echo Join(strTemp, "//")