parsing string, leaving words intact

Discussion in 'ASP General' started by meldrape, Oct 30, 2003.

  1. meldrape

    meldrape Guest

    Hello,

    I need to parse a long string into no more than 30
    character chunks, but I also need to leave the words
    intact. Right now, I am using:

    For intStart = 1 to Len(strOriginal) by 30
    strPrint = Mid$(strOriginal, intStart, 30)
    Print #detailFile, Tab(1), intStart, Tab(4), strPrint
    Next intStart

    and it's fine, but splits up some of the words. If I
    could figure out how to find the preceding space of the
    30 limit, I'd be in good shape. Any thoughts would be
    appreciated. Many thanks in advance.
     
    meldrape, Oct 30, 2003
    #1
    1. Advertising

  2. meldrape

    dlbjr Guest

    Set SP = New StringParser
    SP.SetString "This is a test of the string parsing system",30
    arrChunks = SP.GetChunks
    Set SP = Nothing
    For i = 0 To UBound(arrChunks)
    msgbox arrChunks(i)
    Next

    Class StringParser
    Private mintCounter
    Private mintBreak
    Private marrData
    Private mDic

    Private Sub Class_Initialize()
    mintBreak = 0
    mintCounter = 0
    Set mDic = CreateObject("Scripting.Dictionary")
    End Sub

    Private Sub Class_Terminate()
    Set mDic = Nothing
    End Sub

    Public Function GetChunks()
    GetChunks = mDic.Items
    End Function

    Public Sub SetString(strData,intChunkLength)
    strData = Trim(strData)
    If IsNumeric(intChunkLength) Then
    mintBreak = Abs(intChunkLength) \ 1
    End If
    If Len(strData) > 0 And mintBreak > 0 Then
    marrData = Split(strData," ")
    For i = 0 To UBound(marrData)
    strItem = marrData(i)
    If Len(strChunk) + Len(strItem) <= mintBreak Then
    strChunk = Trim(strChunk & " " & strItem)
    Else
    AddChunk strChunk
    strChunk = strItem
    End If
    Next
    End If
    AddChunk strChunk
    End Sub

    Private Sub AddChunk(strChunk)
    If Len(strChunk) > 0 Then
    mintCounter = mintCounter + 1
    mDic.Add mintCounter,strChunk
    End If
    End Sub
    End Class


    -dlbjr

    Discerning resolutions for the alms
     
    dlbjr, Oct 31, 2003
    #2
    1. Advertising

  3. meldrape

    Ray at Guest

    Here's a vb SCRIPT way.

    <%
    TheString = "I need to parse a long string into no more than 30 character
    chunks, but I also need to leave the words intact. Right now, I am using
    and it's fine, but splits up some of the words. If I could figure out how
    to find the preceding space of the 30 limit, I'd be in good shape. Any
    thoughts would be appreciated. Many thanks in advance."

    '''clear out any double+ spaces
    Do While Instr(TheString, " ") > 0
    TheString = Replace(TheString, " ", " ")
    Loop

    aParts = Split(TheString, " ")
    Redim Preserve aParts(29)

    For q = 0 To 29
    Response.Write aParts(q) & "<br>"
    Next
    %>

    Ray at home





    "meldrape" <> wrote in message
    news:04d801c39f3a$fd9c62f0$...
    > Hello,
    >
    > I need to parse a long string into no more than 30
    > character chunks, but I also need to leave the words
    > intact. Right now, I am using:
    >
    > For intStart = 1 to Len(strOriginal) by 30
    > strPrint = Mid$(strOriginal, intStart, 30)
    > Print #detailFile, Tab(1), intStart, Tab(4), strPrint
    > Next intStart
    >
    > and it's fine, but splits up some of the words. If I
    > could figure out how to find the preceding space of the
    > 30 limit, I'd be in good shape. Any thoughts would be
    > appreciated. Many thanks in advance.
    >
     
    Ray at, Oct 31, 2003
    #3
  4. meldrape

    Ray at Guest

    I think I mis-read your question. Sorry about that. I thought you wanted
    the first 30 words.

    Ray at home

    "Ray at <%=sLocation%>" <myfirstname at lane 34 . komm> wrote in message
    news:%...
    > Here's a vb SCRIPT way.
    >
    > <%
    > TheString = "I need to parse a long string into no more than 30 character
    > chunks, but I also need to leave the words intact. Right now, I am using
    > and it's fine, but splits up some of the words. If I could figure out how
    > to find the preceding space of the 30 limit, I'd be in good shape. Any
    > thoughts would be appreciated. Many thanks in advance."
    >
    > '''clear out any double+ spaces
    > Do While Instr(TheString, " ") > 0
    > TheString = Replace(TheString, " ", " ")
    > Loop
    >
    > aParts = Split(TheString, " ")
    > Redim Preserve aParts(29)
    >
    > For q = 0 To 29
    > Response.Write aParts(q) & "<br>"
    > Next
    > %>
    >
    > Ray at home
    >
    >
    >
    >
    >
    > "meldrape" <> wrote in message
    > news:04d801c39f3a$fd9c62f0$...
    > > Hello,
    > >
    > > I need to parse a long string into no more than 30
    > > character chunks, but I also need to leave the words
    > > intact. Right now, I am using:
    > >
    > > For intStart = 1 to Len(strOriginal) by 30
    > > strPrint = Mid$(strOriginal, intStart, 30)
    > > Print #detailFile, Tab(1), intStart, Tab(4), strPrint
    > > Next intStart
    > >
    > > and it's fine, but splits up some of the words. If I
    > > could figure out how to find the preceding space of the
    > > 30 limit, I'd be in good shape. Any thoughts would be
    > > appreciated. Many thanks in advance.
    > >

    >
    >
     
    Ray at, Oct 31, 2003
    #4
  5. "meldrape" <> wrote in message
    news:04d801c39f3a$fd9c62f0$...
    > Hello,
    >
    > I need to parse a long string into no more than 30
    > character chunks, but I also need to leave the words
    > intact. Right now, I am using:
    >
    > For intStart = 1 to Len(strOriginal) by 30
    > strPrint = Mid$(strOriginal, intStart, 30)
    > Print #detailFile, Tab(1), intStart, Tab(4), strPrint
    > Next intStart
    >
    > and it's fine, but splits up some of the words. If I
    > could figure out how to find the preceding space of the
    > 30 limit, I'd be in good shape. Any thoughts would be
    > appreciated. Many thanks in advance.
    >


    <%
    Const s = "Pneumonoultramicroscopicsilicovolcanoconiosis is the longest
    word in the English dictionary. For those that are interested, it's a
    disease of the lungs which is caused by silica dust.
    123456789012345678901234567890"
    Dim re,arr
    Set re = New RegExp
    re.Global = True
    re.Pattern = "\S{30}|.{1,29}(\s|$)"
    Set matches = re.execute(s)
    Response.Write "<pre>"
    For Each match in matches
    Response.Write vbTab & match.FirstIndex & vbTab & vbTab & vbTab & vbTab
    & match.Value & vbCRLF
    Next
    Response.Write "</pre>"
    %>

    Modifying the code to write to a file instead of to the browser is left
    as an exercise for the reader.

    HTH
    -Chris Hohmann
     
    Chris Hohmann, Oct 31, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TmFk?=
    Replies:
    6
    Views:
    1,073
    =?Utf-8?B?TmFk?=
    Jun 30, 2005
  2. Bell, Kevin
    Replies:
    0
    Views:
    287
    Bell, Kevin
    Oct 20, 2005
  3. Jim Langston
    Replies:
    2
    Views:
    352
  4. BerlinBrown
    Replies:
    6
    Views:
    4,837
  5. pantagruel
    Replies:
    8
    Views:
    481
    Dr John Stockton
    Jul 22, 2006
Loading...

Share This Page