Solved

Splitting a string of text using VB 6.0

Posted on 2011-09-23
11
193 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
 
LVL 45

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now