开发者

Vbscript: Convert text string in small pieces and put it into a array

开发者 https://www.devze.com 2023-04-01 05:27 出处:网络
I need to break a long text string into smaller pieces approximatel开发者_运维技巧y once every 500 characters (not a special character), forming an array of all the sentences and then put them togethe

I need to break a long text string into smaller pieces approximatel开发者_运维技巧y once every 500 characters (not a special character), forming an array of all the sentences and then put them together separated by a specific character (eg / /). Something as follows:

"This text is a very very large text."

So, I get:

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

And finally:

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

Due to my limited knowledge of classic asp, the closest I came to a desired result was the following:

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

However, this returns a repeated and overlapping text string: "this is / / this is a / / this is a text //...

Any idea with vbscript? Thank you!


Without using an array, you can just build the string as you go

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

If you really need an array, you can then split(output, DELIMITER)


Here is a try:

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

精彩评论

暂无评论...
验证码 换一张
取 消