【问题标题】:Vbscript: Convert text string in small pieces and put it into a arrayVbscript:将文本字符串分成小块并将其放入数组中
【发布时间】:2011-08-31 13:42:57
【问题描述】:

我需要将一个长文本字符串分成大约每 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 有任何想法吗?谢谢!

【问题讨论】:

    标签: arrays string text vbscript split


    【解决方案1】:

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

    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)

    【讨论】:

    • 您的代码非常适合我的目的。我真的很感谢你。
    【解决方案2】:

    这是一个尝试:

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

    【讨论】:

    • 感谢您的赞赏。不幸的是,它没有用。我在“ReDim Preserve arrText(UBound(arrText) + 1)”中收到以下错误“数组已修复或暂时锁定”。还有什么建议吗?
    • @afalzolo:我改变了我的代码,所以它将是一个动态数组而不是一个固定数组
    • 不,我得到了同样的错误。另外,如果我将变量声明为字符串(Dim strText as String),我会收到以下错误:“As”中的“End of statement expected”????
    • 我编辑了我的答案,以便再次尝试。我不明白你得到了Array fixed or temporarily locked 错误。我不能在这里测试,但我明天试试
    • @afalzolo JMax 使用 ASP 对象(响应)编写了一些 VBA / VB 代码。 VBscript 仅支持后期绑定(没有“as”关键字和日期类型的声明,例如 Dim strText、Dim iSize),并且 WScript.Echo 用于输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多