Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Splitting a string of text using VB 6.0

Posted on 2011-09-23
11
Medium Priority
?
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

722 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