Parsing a string again

  • Thread starter Thread starter meldrape
  • Start date Start date
M

meldrape

I am still trying to parse a string into 30 character chunks, but now
it's more complicated because I need a way to parse it into no more
than 30 characters BUT leaving words intact. I hope this makes sense.
Thanks in advance for any advice given.
 
meldrape said:
I am still trying to parse a string into 30 character chunks, but now
it's more complicated because I need a way to parse it into no more
than 30 characters BUT leaving words intact. I hope this makes sense.
Thanks in advance for any advice given.

Define "word", for this purpose. Are words separated only by blanks, or
do we have to cope with punctuation as well?
 
I would use InStrRev, set it to start at char 30 and set
it to find a space. Then make your block = left part of
the string, limited by the InStrRev value.

Dim strWhole As String
Dim counter As Integer
Dim strPieces(1 To 50) As String
Dim spacePos As Long

strWhole = "{yourstring}"
counter = 1
Do Until Len(strWhole) <= 30
spacePos = InStrRev(strWhole, " ", 30) 'find the
nearest space
If spacePos = 0 Then spacePos = 30 'if none, grab 30
char
strPieces(counter) = Left(strWhole, spacePos) 'put the
piece into the array
counter = counter + 1 'increment the counter
strWhole = Right(strWhole, Len(strWhole) -
spacePos) 'remove that piece from the master strnig
Loop
strPieces(counter) = strWhole 'grab the last bit

Hope this helps!
 
-----Original Message-----


Define "word", for this purpose. Are words separated only by blanks, or
do we have to cope with punctuation as well?

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


.
Yes, words are separated only by blanks. You don't have
to cope with punctuation. I haven't tried the example
from the previous post yet.

Here's an example of a string:

TAKE-OFF AND LAYOUTS ARE BASED ON PLANS AND
SPECIFICATIONS PROVIDED. CHANGES AND ADDITIONS ARE THE
RESPONSIBILITY OF THE BUILDER.
 
Dirk Goldgar said:
Define "word", for this purpose. Are words separated only by blanks, or
do we have to cope with punctuation as well?

Yes, the words are separated only by blanks. No coping with
punctuation. Here's an example of the string:

TAKE-OFF AND LAYOUTS ARE BASED ON PLANS AND
SPECIFICATIONS PROVIDED. CHANGES AND ADDITIONS ARE THE
RESPONSIBILITY OF THE BUILDER.

Thanks again!!
 
meldrape said:
to cope with punctuation. I haven't tried the example
from the previous post yet.

Here's an example of a string:

TAKE-OFF AND LAYOUTS ARE BASED ON PLANS AND
SPECIFICATIONS PROVIDED. CHANGES AND ADDITIONS ARE THE
RESPONSIBILITY OF THE BUILDER.


Here's a subroutine you can use as a model. I've only tested it a
little bit, though.

'----- start of code -----

Sub ParseWords30(strSource As String)

Dim astrWords() As String
Dim strOut As String
Dim lngA As Long
Dim lngB As Long

astrWords = Split(strSource, " ")

lngA = LBound(astrWords)
Do While lngA <= UBound(astrWords)

strOut = astrWords(lngA)
For lngB = lngA + 1 To UBound(astrWords)
If Len(strOut) + Len(astrWords(lngB)) + 1 > 30 Then
Exit For
Else
strOut = strOut & " " & astrWords(lngB)
End If
Next lngB

lngA = lngB

' Print out the output string in 30-character pieces.
Do Until Len(strOut) = 0
Debug.Print Left(strOut, 30)
strOut = Mid(strOut, 31)
Loop

Loop 'Do While lngA <= UBound(astrWords)

End Sub
'----- end of code -----

In the immediate window, this is what I get:

ParseWords30 "TAKE-OFF AND LAYOUTS ARE BASED ON PLANS AND SPECIFICATIONS
PROVIDED. CHANGES AND ADDITIONS ARE THE RESPONSIBILITY OF THE BUILDER."
TAKE-OFF AND LAYOUTS ARE BASED
ON PLANS AND SPECIFICATIONS
PROVIDED. CHANGES AND
ADDITIONS ARE THE
RESPONSIBILITY OF THE BUILDER.
 
Back
Top