Solved

Splitting a string of text using VB 6.0

Posted on 2011-09-23
11
196 Views
Last Modified: 2012-05-12
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
Comment
Question by:GarySB
11 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36587000
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
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 36587047
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
 

Author Comment

by:GarySB
ID: 36587174
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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 46

Expert Comment

by:Martin Liss
ID: 36587214
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36587236
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
 

Author Comment

by:GarySB
ID: 36587482
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36590344
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
 

Author Comment

by:GarySB
ID: 36606720
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 36615982
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
 

Author Comment

by:GarySB
ID: 36694423
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36707990
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question