Asp classic 在ASP Classic中拆分字符串

Asp classic 在ASP Classic中拆分字符串,asp-classic,Asp Classic,这是我的字符串: Lorem ipsum dolor sit amet,是一位杰出的献身者。纳拉姆·艾利特·拉克斯,达官贵人奎斯·劳里特·农,厄洛斯的诅咒。这是一种新的治疗方法。佩伦特式居住者morbi tristique Sentecus et netus et malesuada以turpis egestas闻名。前庭是生命的一部分 我需要拆分它每100个字符(完整的话只),直到所有的字符都用完了 因此,我们最终会: Lorem ipsum dolor sit amet,是一位杰出的献身者。

这是我的字符串:

Lorem ipsum dolor sit amet,是一位杰出的献身者。纳拉姆·艾利特·拉克斯,达官贵人奎斯·劳里特·农,厄洛斯的诅咒。这是一种新的治疗方法。佩伦特式居住者morbi tristique Sentecus et netus et malesuada以turpis egestas闻名。前庭是生命的一部分

我需要拆分它每100个字符(完整的话只),直到所有的字符都用完了

因此,我们最终会:

Lorem ipsum dolor sit amet,是一位杰出的献身者。纳拉姆·艾利特·拉库斯,拉奥里特·农的贵人

厄洛斯女神。这是一种新的治疗方法。佩伦茨克居民莫比·特里斯蒂克

塞内特斯、内特斯和马莱苏阿达是著名的埃吉斯塔斯土尔皮斯人。前庭是生命的一部分


有什么好办法吗?

首先,您可能希望使用空格字符作为分隔符拆分字符串。然后从空字符串开始,迭代数组中的每个单词,将每个单词连接到新字符串,直到字数超过100:

str = "Lorem ipsum ...."
words = Split(str)
stringSection = ""
wordCounter = 0

FOR EACH word IN words
   stringSection = stringSection & word
   wordCounter = wordCounter + 1

   IF wordCounter >= 100 THEN
      Response.Write(stringSection & "<BR /><BR />")

      wordCounter = 0
      stringSection = ""
   ELSE
      stringSection = stringSection & " "
   END IF
NEXT

Response.Write(stringSection & "<BR /><BR />")
str=“Lorem ipsum…”
单词=拆分(str)
stringSection=“”
字计数器=0
字里行间
stringSection=stringSection和word
wordCounter=wordCounter+1
如果字计数器>=100,则
Response.Write(stringSection&“

”) 字计数器=0 stringSection=“” 其他的 stringSection=stringSection&“ 如果结束 下一个 Response.Write(stringSection&“

”)

请注意,要处理最后一个
stringSection
,最后一个
响应.Write
是必需的,即使它可能没有超过100个字。

首先,您可能需要使用空格字符作为分隔符拆分字符串。然后从空字符串开始,迭代数组中的每个单词,将每个单词连接到新字符串,直到字数超过100:

str = "Lorem ipsum ...."
words = Split(str)
stringSection = ""
wordCounter = 0

FOR EACH word IN words
   stringSection = stringSection & word
   wordCounter = wordCounter + 1

   IF wordCounter >= 100 THEN
      Response.Write(stringSection & "<BR /><BR />")

      wordCounter = 0
      stringSection = ""
   ELSE
      stringSection = stringSection & " "
   END IF
NEXT

Response.Write(stringSection & "<BR /><BR />")
str=“Lorem ipsum…”
单词=拆分(str)
stringSection=“”
字计数器=0
字里行间
stringSection=stringSection和word
wordCounter=wordCounter+1
如果字计数器>=100,则
Response.Write(stringSection&“

”) 字计数器=0 stringSection=“” 其他的 stringSection=stringSection&“ 如果结束 下一个 Response.Write(stringSection&“

”)

请注意,最后一个
响应。Write
是处理最后一个
stringSection
所必需的,即使它可能没有超过100个单词。

由于Daniel使用与我描述类似的实际代码进行了回复,我将采用不同的建议。我可能和伯爵差一个字。此代码打印开始/结束偏移和子字符串。您需要做的是修改此项以将字符串保存在数组中:

<%

Dim LoremIpsum

    LoremIpsum = "Lorem ipsum dolor sit amet....."

    Response.Write LoremIpsum & "<br>"

    SplitWords LoremIpsum, 100


Function SplitWords(text, maxlen)

Dim c, i, j, l

    l = Len(text)
    i = 1
    j = maxlen

    Do While (j < l And Response.IsClientConnected)

        c = Mid(text, j, 1)

        Do While (c <> " " And j > i)

            j = j - 1

            c = Mid(text, j, 1)

        Loop

        Response.Write(i & "<br>")
        Response.Write(j & "<br>")

        s = Mid(text, i, j-i)

        Response.Write(s & "<br>")

        i = j
        j = j + maxlen

    Loop


End Function

%>
i)
j=j-1
c=Mid(文本,j,1)
环
响应。写入(i&“
”) 响应。写入(j&“
”) s=Mid(文本,i,j-i) 响应。写入(s&“
”) i=j j=j+maxlen 环 端函数 %>
由于Daniel回复的代码与我的描述类似,我将采用不同的建议。我可能和伯爵差一个字。此代码打印开始/结束偏移和子字符串。您需要做的是修改此项以将字符串保存在数组中:

<%

Dim LoremIpsum

    LoremIpsum = "Lorem ipsum dolor sit amet....."

    Response.Write LoremIpsum & "<br>"

    SplitWords LoremIpsum, 100


Function SplitWords(text, maxlen)

Dim c, i, j, l

    l = Len(text)
    i = 1
    j = maxlen

    Do While (j < l And Response.IsClientConnected)

        c = Mid(text, j, 1)

        Do While (c <> " " And j > i)

            j = j - 1

            c = Mid(text, j, 1)

        Loop

        Response.Write(i & "<br>")
        Response.Write(j & "<br>")

        s = Mid(text, i, j-i)

        Response.Write(s & "<br>")

        i = j
        j = j + maxlen

    Loop


End Function

%>
i)
j=j-1
c=Mid(文本,j,1)
环
响应。写入(i&“
”) 响应。写入(j&“
”) s=Mid(文本,i,j-i) 响应。写入(s&“
”) i=j j=j+maxlen 环 端函数 %>
我需要计算空格,并将其作为一个函数……以下是我的想法

Function wordSubstring(txtString,maxLen) words = Split(txtString," ") charCounter = 0 stringSection = "" For Each word IN words stringSection = stringSection & word charCounter = len(stringSection) if charCounter >= maxLen Then wordSubstring=stringSection exit For else stringSection = stringSection & " " end If Next wordSubstring = stringSection End Function 函数wordSubstring(txtString,maxLen) words=Split(txtString,“”) charCounter=0 stringSection=“” 字里行间 stringSection=stringSection和word charCounter=len(stringSection) 如果charCounter>=maxLen,则 wordSubstring=stringSection 退出 其他的 stringSection=stringSection&“ 如果结束 下一个 wordSubstring=stringSection 端函数
我需要计算空间,并将其作为一个函数…这是我的想法

Function wordSubstring(txtString,maxLen) words = Split(txtString," ") charCounter = 0 stringSection = "" For Each word IN words stringSection = stringSection & word charCounter = len(stringSection) if charCounter >= maxLen Then wordSubstring=stringSection exit For else stringSection = stringSection & " " end If Next wordSubstring = stringSection End Function 函数wordSubstring(txtString,maxLen) words=Split(txtString,“”) charCounter=0 stringSection=“” 字里行间 stringSection=stringSection和word charCounter=len(stringSection) 如果charCounter>=maxLen,则 wordSubstring=stringSection 退出 其他的 stringSection=stringSection&“ 如果结束 下一个 wordSubstring=stringSection 端函数
谢谢你们两位的回答。丹尼尔:charCounter的定义在哪里?这是真的有效还是仅仅是一个例子?@Sam:这是一个打字错误。。。修正:)谢谢你们的回答。丹尼尔:charCounter的定义在哪里?这是真的有效还是仅仅是一个例子?@Sam:这是一个打字错误。。。固定:)