2011-08-31 82 views
0

我需要将一个长文本字符串分成几个大小为500的字符(不是特殊字符),形成一个包含所有句子的数组,然后放入它们一起被一个特定的字符分隔(例如/ /)。如下:Vbscript:将文本字符串转换为小块并将其放入数组

“这段文字是一个非常大的文字。”

所以,我得到:

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 

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

任何想法与VBScript?谢谢!

回答

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, "//") 
+0

感谢您的赞赏。不幸的是,它没有奏效。 'ReDim Preserve arrText(UBound(arrText)+ 1)'中出现以下错误“Array fixed or temporarily locked”。还有什么建议? – afazolo

+0

@afalzolo:我改变了我的代码,所以它将是一个动态数组而不是固定的 – JMax

+0

Nop,我得到了同样的错误。另外,如果我将变量声明为一个字符串(Dim strText **作为** String),则会出现以下错误:“As”中的“期望语句结束”? – afazolo

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)

+0

您的代码完美适用于我的目的。我真的很感谢你。 – afazolo