tags:

views:

82

answers:

2

So here's my string:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam elit lacus, dignissim quis laoreet non, cursus id eros. Etiam lacinia tortor vel purus eleifend accumsan. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Quisque bibendum vestibulum nisl vitae volutpat.

I need to split it every 100 characters (full words only) until all the characters are used up.

So we'd end up with:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam elit lacus, dignissim quis laoreet non,

and

cursus id eros. Etiam lacinia tortor vel purus eleifend accumsan. Pellentesque habitant morbi tristique

and

senectus et netus et malesuada fames ac turpis egestas. Quisque bibendum vestibulum nisl vitae volutpat.

Any ideas on the best way to do that?

+2  A: 

First you may want to split your string with the space character as a delimiter. Then start with an empty string, iterate over each word in the array, concatenate each word to the new string until the number of words exceeds 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 />")

Note that the last Response.Write is necessary to handle the last stringSection, even though it might have not exceeded the 100 words.

Daniel Vassallo
You beat me to it :)
thomask
By 15 seconds :)
Daniel Vassallo
Thank you both for your answers. Daniel: Where is charCounter definied? Should this actually work or was it just an example?
Sam
@Sam: It was a typo... fixed :)
Daniel Vassallo
+4  A: 

Since Daniel replied with actual code similar to my description, I'm gonna go with a different suggestion. I might be one character off with count. This code prints the start/end offsets and the substrings. What YOU need to do is modify this to save the strings in an array instead:

<%

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

%>
thomask