Link to home
Start Free TrialLog in
Avatar of pawnit
pawnit

asked on

Word Wrapping With CrLf

Hi Experts,

I am trying to achieve some word wrapping in my application. I have managed to get the word wrapping to work, well that is if there are no carriage return line feeds in the initial string. If there are the word wrapping goes nuts basiclly. This is the code i have so far

Dim txt As String = "Text Goes Here Text Goes Here Text Goes Here" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "More Text Here Text Goes Here Text Goes Here"
        Dim ll As Integer = 40
        Dim tl As Integer = Len(txt)
        Dim block As String = String.Empty
        Dim ptxt = String.Empty
        Do Until tl <= ll
            block = Microsoft.VisualBasic.Left(txt, ll)
            For f As Integer = ll To 1 Step -1
                Dim str = Mid(block, f, 1)
                If Trim(str) = String.Empty Then
                    ptxt += Microsoft.VisualBasic.Left(block, (f - 1))
                    ptxt += vbNewLine
                    txt = Mid(txt, (f + 1), tl)
                    tl = Len(txt)
                    Exit For
                End If
            Next
        Loop

        Console.WriteLine(ptxt & txt)

Open in new window


The console output i get as a result of this is:

Text Goes Here Text Goes Here Text Goes
Here

More Text Here Text Goes Here
Text Goes Here

Theroretically the top 2 lines outputed in the console should match the bottom 2. However it doesnt. Any ideas anyone?
Avatar of developmentguru
developmentguru
Flag of United States of America image

There are two ways to approach this problem.

1) strip the CR and LF characters from the text and then word wrap it.
2) accept the CR and LF characters where they appear and do the word wrap starting right after each combination is found.

I assume you are trying the second approach.   It also looks like you have a desired maximum line length of 40 characters.

I am going to try to rewrite your code, for readability.  Redoing the code was primarily to help understand what you have already done.  The 2 character variable names are really not good practive.  I will post again, with new code.
Dim TestLine As String = "Text Goes Here Text Goes Here Text Goes Here" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "More Text Here Text Goes Here Text Goes Here"
  Dim LineLength As Integer = 40
  Dim TextLength As Integer = Len(txt)
  Dim Block As String = String.Empty
  Dim FormattedLine = String.Empty

  Do Until TextLength <= LineLength
    Block = Microsoft.VisualBasic.Left(TestLine, ll)
    For I As Integer = ll To 1 Step -1
      Dim str = Mid(block, I, 1)
      If Trim(str) = String.Empty Then
        FormattedText += Microsoft.VisualBasic.Left(block, (f - 1))
        FormattedText += vbNewLine
        TestLine = Mid(txt, (f + 1), tl)
        TextLength = Len(TestLine)
        Exit For
      End If
    Next
  Loop

Console.WriteLine(FormattedText & TestLine)

Open in new window

practive = practice
Avatar of aikimark
@DevelopmentGuru,
You missed some of the variable name replacements for new variable names.

Dim TestLine As String = "Text Goes Here Text Goes Here Text Goes Here" & vbCrLf & vbCrLf & "More Text Here Text Goes Here Text Goes Here"
  Dim LineLength As Integer = 40
  Dim TextLength As Integer = Len(txt)
  Dim Block As String = String.Empty
  Dim FormattedLine = String.Empty

  Do Until TextLength <= LineLength
    Block = Microsoft.VisualBasic.Left(TestLine, LineLength)
    For I As Integer = LineLength To 1 Step -1
      Dim str = Mid(Block , I, 1)
      If Trim(str) = String.Empty Then
        FormattedText += Microsoft.VisualBasic.Left(Block , (I - 1))
        FormattedText += vbNewLine
        TestLine = Mid(TestLine, (I + 1), TextLength)
        TextLength = Len(TestLine)
        Exit For
      End If
    Next
  Loop

Console.WriteLine(FormattedText & TestLine)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of developmentguru
developmentguru
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pawnit
pawnit

ASKER

@DevelopmentGuru,

Thanks for the Delphi Code, I have never used delphi before but i kind on get the jist of what you are doing. Ill attempt to write the same thing in vb. Ill repost what i come up with and see if that works.
Avatar of pawnit

ASKER

@DevelopmentGuru,

I have converted over the code you wrote in to my understanding of it in VB. It seems to be working perfectly so far :) Thank you.

Module Module1
    Const TestLine As String = "Text Goes Here Text Goes Here Text Goes Here" & Chr(13) & Chr(10) & Chr(13) & Chr(10) _
    & "More Text Here Text Goes Here Text Goes Here"
    Const MaxLineLength As Integer = 40

    Sub Main()

        Dim WorkLine As String
        Dim TextLength As Integer
        Dim Block As String
        Dim FormattedLine As String = String.Empty
        Dim NextCRLF As Integer
        Dim SpaceLoc As Integer

        WorkLine = TestLine

        Do While Len(WorkLine) > 0
            'How far to next CRLF
            NextCRLF = InStr(WorkLine, Chr(13) & Chr(10))

            If (NextCRLF <= 41) And (NextCRLF <> 0) Then 'Take the line as it is
                Block = Mid(WorkLine, 1, NextCRLF - 1)
                WorkLine = Mid(WorkLine, NextCRLF + 2, Len(WorkLine))
            Else
                Block = String.Empty
                Do Until (WorkLine = String.Empty) Or ((Len(Block) + SpaceLoc) > 40)
                    SpaceLoc = InStr(WorkLine, " ")
                    'If no more spaces then use end of line

                    If SpaceLoc = 0 Then
                        SpaceLoc = Len(WorkLine) + 1
                    End If

                    If (Len(Block) + SpaceLoc - 1) < 40 Then
                        If Block IsNot String.Empty Then
                            Block &= " "
                        End If
                        Block &= Mid(WorkLine, 1, SpaceLoc - 1)
                        WorkLine = Mid(WorkLine, SpaceLoc + 1, Len(WorkLine))
                    End If
                Loop
            End If
            FormattedLine &= Block & Chr(13) & Chr(10)
        Loop

        Console.WriteLine(FormattedLine)
        Console.ReadLine()
    End Sub

End Module

Open in new window

I would like to take the opportunity to thank the question's author for behaving in an exemplary manner.  

Many of the people that come on EE seeking answers, want the experts to do the work for them.  Many would have complained about the code I posted since it did not amount to a complete solution in the language they preferred.  I really enjoy working with people who are looking for answers and are not allergic to working with the experts to find their answer.  Well done!

I also think it was nice of you to post the code for future VB users to find.  Once again, well done.
Avatar of pawnit

ASKER

Thank you for the nice words DevelopmentGuru, I have now hit a brick wall and i have no idea what is happening here.

The first screenshot which i have attached is the code running in a module of a console application. It appears to be wrapping correctly to 74 characters per line.

I pretty much well copied the code and made it into a class for my winform app and when executed it does not appear to wrap correclty. This is displayed in the 2nd image attachment.

Anyone have any ideas? Unless i am confused there should not be any difference in the output.

Snip1.PNG
Snip2.PNG
@pawnit

Did you mean to accept a solution to your problem?  Your last comment indicates that you do not have a properly working solution.
Avatar of pawnit

ASKER

Thanks for all your help guys! The error was on my part! I did mean to accept the accepted solution :)