Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with splitting a string (more complex than the Split function)

Status
Not open for further replies.

millerk

Programmer
Jul 5, 2002
133
0
0
US
I have a string variable that will usually be several hundred characters long. It is basically a paragraph that will be displayed on a report and included in an electronic file. The electronic file requires that it be split into multiple lines, each of which are no longer than 80 characters. But I don't want it to split in the middle of the word.

One way I've tried it is to split on the space character and get an array of words. Then I create another array, where each element will be 1 line of text (for the e-file). Then I loop through the words, and for each one, if it will fit on the line without going over the max length, I add it to the line, and if not, I create another line and add it there.

This works as long as the max length for each chunk is greater than the length of each individual word. But what I'd like is to have a function to never exceed the max lenght for each chunk, even if it means it has to split the string somewhere other than on a space.

Any ideas?
 
I think you've got a good start. Just sounds like you need to check see if it will fit on a new full 80 char line prior to creating it.

If not, fill what you can of the old line and create a Do Until loop that creates new lines as required until there are no more chars left in the word.

If Yes, then continue as you had been doing.

Clear as Mud? ;o)

Senior Software Developer
<A href=" face=Century>Scout Software LLC</FONT></A>
 
Are you saying if the "last" word is a long one then you may need to split the word?
If so could you just check the length of each word and work with that?
Good luck,
djj

If you find one please post a solution.
 
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim line As String = "", line_80 As String = "", line_rest As String = "", pos As Integer

line = "I have a string variable that will usually be several hundred characters long. " & _
"It is basically a paragraph that will be displayed on a report and included in an " & _
"electronic file. The electronic file requires that it be split into multiple " & _
"lines, each of which are no longer than 80 characters. But I don't want it to " & _
"split in the middle of the word. One way I've tried it is to split on the " & _
"space character and get an array of words. Then I create another array, " & _
"where each element will be 1 line of text (for the e-file). Then I loop " & _
"through the words, and for each one, if it will fit on the line without " & _
"going over the max length, I add it to the line, and if not, I create " & _
"another line and add it there. This works as long as the max length for " & _
"each chunk is greater than the length of each individual word. But what " & _
"I'd like is to have a function to never exceed the max lenght for each " & _
"chunk, even if it means it has to split the string somewhere other than " & _
"on a space. Any ideas?"

Do
' Take 80 characters and look for the last space
pos = InStrRev(Mid(line, 1, 80), " ")
line_80 = Mid(line, 1, pos)
line_rest = Mid(line, pos + 1)
line = line_rest

Debug.Print(line_80)
Loop While Len(line) >= 80

Debug.Print(line)
End Sub
End Class
 
Try This:

Code:
    Public Function getFormatedText() As String
        Dim sText As String = ""
        Dim line As String = "I have a string variable that will usually be several hundred characters long.  " & _
               "It is basically a paragraph that will be displayed on a report and included in an " & _
               "electronic file.  The electronic file requires that it be split into multiple " & _
               "lines, each of which are no longer than 80 characters.  But I don't want it to " & _
               "split in the middle of the word.  One way I've tried it is to split on the " & _
               "space character and get an array of words.  Then I create another array, " & _
               "where each element will be 1 line of text (for the e-file).  Then I loop " & _
               "through the words, and for each one, if it will fit on the line without " & _
               "going over the max length, I add it to the line, and if not, I create " & _
               "another line and add it there.  This works as long as the max length for " & _
               "each chunk is greater than the length of each individual word.  But what " & _
               "I'd like is to have a function to never exceed the max lenght for each " & _
               "chunk, even if it means it has to split the string somewhere other than " & _
               "on a space.  Any ideas?"

        Dim sWords As String() = line.Split(" ")
        Dim sWord As String
        Dim nvcLines As New Collections.Specialized.NameValueCollection
        nvcLines.Add("0", "")
        For Each sWord In sWords
            If nvcLines(nvcLines.Count - 1).Length + sWord.Length < 80 Then
                nvcLines.Set((nvcLines.Count - 1).ToString, nvcLines(nvcLines.Count - 1) & sWord & " ")
            ElseIf sWord.Length > 80 Then
                nvcLines.Set((nvcLines.Count - 1).ToString, nvcLines(nvcLines.Count - 1) & sWord.Substring(0, 80 - nvcLines(nvcLines.Count - 1).Length) & "-")
                sWord = sWord.Substring(80 - nvcLines(nvcLines.Count - 1).Length + 1)
                Do While sWords.Length > 0
                    If sWord.Length > 80 Then
                        nvcLines.Add((nvcLines.Count).ToString, sWord.Substring(0, 80) & "-")
                        sWord = sWord.Substring(80 - nvcLines(nvcLines.Count - 1).Length + 1)
                    Else
                        nvcLines.Add((nvcLines.Count).ToString, sWord & " ")
                    End If
                Loop
            Else
                nvcLines.Add((nvcLines.Count).ToString, sWord & " ")
            End If
        Next
        Dim i As Integer
        For i = 0 To nvcLines.Count - 1
            sText += nvcLines(i.ToString) & ControlChars.CrLf
        Next
        Debug.Print(sText)
        Return sText
    End Function

Senior Software Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top