2011-08-31 78 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