Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Splitting a string of text using VB 6.0

I have a variable tmp$ that contains an entire text page of appox. 300 words.
It needs to be divided up into the P$ in lengths of 40 characters max.
Right now it is placing the entire tmp$ into P$.
I not only need it to be no more than 40 characters, it has to not split a word in the middle.
In other words it has to stop on a space dividing a word before the 40 character mark.
Also "On the "  would come out as "On the" Should remove end space from sentence.
How can my code below be adjusted to resolve this?  Thank You

ORIGINAL SAMPLE
      tmp$="On the first friday of every month our staff members ..."

STRAIGHT 40 RESULT
     P$(0)="On the first friday of every month our s"

DESIRED
     P(0)$="On the first friday of every month our"
     P(1)$="staff members ..."

Dim vSplitTemp As Variant
    vSplitTemp = Split(tmp$, vbCrLf)
    For x% = LBound(vSplitTemp) To UBound(vSplitTemp)
        P$(x% + 1) = vSplitTemp(x%)
    Next x%
   
0
GarySB
Asked:
GarySB
1 Solution
 
GrahamSkanCommented:
Is this what you are looking for?
Sub SplitIntoShortLines()
    Dim strLine As String
    Dim strShortLines() As String
    Dim strWords() As String
    Dim w As Integer
    Dim l As Integer
    
    strLine = "Aloysius wears his long silver hair in a pony tail. Relaxed and confident, he has an attractive, commanding presence and is locally known as The Magus"
    strWords() = Split(strLine, " ")
    Do Until w = UBound(strWords())
        ReDim strShortLines(l)
        Do Until Len(strShortLines(l)) + Len(strWords(w)) > 38
            strShortLines(l) = strShortLines(l) & " " & strWords(w)
            If w < UBound(strWords()) Then
                w = w + 1
            Else
                Exit Do
            End If
        Loop
        Debug.Print strShortLines(l)
        l = l + 1
    Loop
End Sub

Open in new window

0
 
HooKooDooKuCommented:
My first thought is simply using the Instr function to find where spaces are in the string.  The function allows you to specify a starting point to look for a match, and you simply have to keep searching for spaces recalling the last space and the most recent space.  When you've found a space pass 40 from where you started, you use the previous space.

The code would look something like this:
Dim StartPos As Long
Dim LastPos As Long
Dim Pos As Long
Dim Index As Integer

    Index = 0
    StartPos = 1
    LastPos = 1
    Pos = InStr(StartPos + 1, tmp$, " ")
    While Pos > 0
        If (Pos - StartPos) > 40 Then
            P$(Index) = Mid$(tmp$, StartPos, LastPos - StartPos)
            StartPos = LastPos + 1
            Index = Index + 1
        End If
        Pos = InStr(Pos + 1, tmp$, " ")
    Loop
    P$(Index) = Mid$(tmp$, StartPos)
0
 
GarySBAuthor Commented:
GrahamSkan: It works well except that it cuts the sentence short when it encounters a comma.  Can another IF test be added to factor in this condition?

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Martin LissRetired ProgrammerCommented:
You could do this to solve the comma problem.

strLine = Replace("Aloysius wears his long silver hair in a pony tail. Relaxed and confident, he has an attractive, commanding presence and is locally known as The Magus", ",", "")

Open in new window


and do similar for other punctuation.
0
 
GrahamSkanCommented:
I can't reproduce your comma problem. My text already has a comma, and this is the output that I get.

Aloysius wears his long silver hair in
a pony tail. Relaxed and confident, he
has an attractive, commanding presence
and is locally known as The Magus
0
 
GarySBAuthor Commented:
I misunderstood the problem.  It is not the comma. What is happening is that when it sees a return between the paragraphs, it counts the return character as one.  
When the paragraph ends, it needs to be the end of the sentence regardless of the count.
For example:

This is a test.

Continue sentence BREAKS BECAUSE IT ADDS BOTH LINES AS ONE.  'This is a test." needs to be the end of the line even though it is much less than 40.  'Continue needs to start a new line.  The carriage return inbetween needs to be ignored.


0
 
GrahamSkanCommented:
That stipulation uses the same logic, but needs another 'paragraph' loop to contain the existing loops.
Sub SplitIntoShortLines()
    Dim strText As String
    Dim strShortLines() As String
    Dim strWords() As String
    Dim strParagraphs() As String
    Dim w As Integer
    Dim l As Integer
    Dim p As Integer
    
    strText = "Midsomer Murders - The Magician's Nephew - MainCharacters/Roles"
    strText = strText & vbCrLf & "Aloysius Wilmington - Stuart Wilson"
    strText = strText & vbCrLf & "Aloysius wears his long silver hair in a pony tail. Relaxed and confident, he has an attractive, commanding presence and is locally known as The Magus."
    strText = strText & vbCrLf & "Simon Wilmington - Dominic Rowan"
    strText = strText & vbCrLf & "The feckless and wayward nephew to Aloysius, Simon lives with his uncle and is his heir."
    strText = strText & vbCrLf & "Ernest Balliol - Ronald Pickup"
    strText = strText & vbCrLf & "Ernest is the proprietor of Magicmaister.com, purveyor of magical artefacts to believers. He cultivates the corduroy look of the countryman."
    
    Debug.Print strText
    strParagraphs = Split(strText, vbCrLf)
    For p = 0 To UBound(strParagraphs)
        strWords() = Split(strParagraphs(p), " ")
        l = 0
        w = 0
        Do Until w = UBound(strWords())
            ReDim strShortLines(l)
            Do Until Len(strShortLines(l)) + Len(strWords(w)) > 38
                strShortLines(l) = strShortLines(l) & " " & strWords(w)
                If w < UBound(strWords()) Then
                    w = w + 1
                Else
                    Exit Do
                End If
            Loop
            strShortLines(l) = LTrim$(strShortLines(l))
            Debug.Print strShortLines(l)
            l = l + 1
        Loop
    Next p
End Sub

Open in new window

0
 
GarySBAuthor Commented:
I'm getting error subscript out of range on line:
         Do Until Len(strShortLines(l)) + Len(strWords(w)) > 38
It is the strWords(w) that the error is on.
w equal to zero
When it has your text it works fine.  When i make strText variable equal my text with strText = tmp$ it give an error.
Please advice

0
 
GrahamSkanCommented:
Ah. That would be caused by empty paragraphs. I don't know how you want them treated, but this outputs a blank line:


Sub SplitIntoShortLines()
    Dim strText As String
    Dim strShortLines() As String
    Dim strWords() As String
    Dim strParagraphs() As String
    Dim w As Integer
    Dim l As Integer
    Dim p As Integer
    
    strText = "Midsomer Murders - The Magician's Nephew - MainCharacters/Roles"
    strText = strText & vbCrLf & vbCrLf & "Aloysius Wilmington - Stuart Wilson"
    strText = strText & vbCrLf & "Aloysius wears his long silver hair in a pony tail. Relaxed and confident, he has an attractive, commanding presence and is locally known as The Magus."
    strText = strText & vbCrLf & vbCrLf & "Simon Wilmington - Dominic Rowan"
    strText = strText & vbCrLf & "The feckless and wayward nephew to Aloysius, Simon lives with his uncle and is his heir."
    strText = strText & vbCrLf & vbCrLf & "Ernest Balliol - Ronald Pickup"
    strText = strText & vbCrLf & "Ernest is the proprietor of Magicmaister.com, purveyor of magical artefacts to believers. He cultivates the corduroy look of the countryman."
    
    Debug.Print strText
    strParagraphs = Split(strText, vbCrLf)
    For p = 0 To UBound(strParagraphs)
        If Len(strParagraphs(p)) > 0 Then
            strWords() = Split(strParagraphs(p), " ")
            l = 0
            w = 0
            Do Until w = UBound(strWords())
                ReDim strShortLines(l)
                Do Until Len(strShortLines(l)) + Len(strWords(w)) > 38
                    strShortLines(l) = strShortLines(l) & " " & strWords(w)
                    If w < UBound(strWords()) Then
                        w = w + 1
                    Else
                        Exit Do
                    End If
                Loop
                strShortLines(l) = LTrim$(strShortLines(l))
                Debug.Print strShortLines(l)
                l = l + 1
            Loop
        Else
            ReDim strShortLines(0)
            strShortLines(0) = ""
            Debug.Print strShortLines(0)
        End If
    Next p
End Sub

Open in new window

0
 
GarySBAuthor Commented:
This should be the last thing.
The routine worked, but i really need to get rid of the blank line for an empty paragraph.
I commented out the two ReDim statements because they give a Array already defined error.
My strShortlines variable is defined as: Global strShortlines$ (120)
If the blank line can be go, it will be perfect!
0
 
GrahamSkanCommented:
You can only Redim an array if it is declared without a size:

Global strShortlines$ ()
or
Public  strShortlines () As String

Lines 40 to 42 produce the empty paragraph, so you can delete them.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now