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?
pawnitAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

developmentguruPresidentCommented:
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

0
developmentguruPresidentCommented:
practive = practice
0
aikimarkCommented:
@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

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

developmentguruPresidentCommented:
I appreciate the "heads up" since I have not done VB in quite a while.  I am going to post some Delphi code that does what is intended.  I can attempt to translate it but I think the method is what is really needed.  Let us know if you want a translation and perhaps another expert will oblige.  If not, I can get you close.

Keep in mind, this may not be the fastest way to handle the problem.  The code I am posting here is to show the method in an easy to follow manner.
procedure TForm1.Button1Click(Sender: TObject);
const
  TestLine = 'Text Goes Here Text Goes Here Text Goes Here' + #13#10 + #13#10 +
    'More Text Here Text Goes Here Text Goes Here';
  MaxLineLength = 40;

var
  WorkLine : string;
  TextLength : integer;
  Block : string;
  FormattedLine : string;
  NextCRLF : integer;
  SpaceLoc : integer;

begin
  WorkLine := TestLine;
  while Length(WorkLine) > 0 do
    begin
      //How far to next CRLF?
      NextCRLF := Pos(#13#10, WorkLine);

      if (NextCRLF <= 41) and (NextCRLF <> 0) then //take the line as is
        begin
          Block := copy(WorkLine, 1, NextCRLF - 1);
          WorkLine := Copy(WorkLine, NextCRLF + 2, Length(WorkLine));
        end
      else
        begin
          //build the new line by taking up to 40 characters using whole word
          //size chunks
          Block := '';
          repeat
            SpaceLoc := Pos(' ', WorkLine);

            //if there are no more spaces then use end of line
            if SpaceLoc = 0 then
              SpaceLoc := Length(WorkLine) + 1;

            if Length(Block) + SpaceLoc - 1 < 40 then
              begin
                if Block <> '' then
                  Block := Block + ' ';
                Block := Block + copy(WorkLine, 1, SpaceLoc - 1);
                WorkLine := Copy(WorkLine, SpaceLoc + 1, Length(WorkLine));
              end;
          until (WorkLine = '') or (Length(Block) + SpaceLoc > 40);
        end;

      Memo1.Lines.Add(Block);
    end;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pawnitAuthor Commented:
@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.
0
pawnitAuthor Commented:
@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

0
developmentguruPresidentCommented:
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.
0
pawnitAuthor Commented:
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
0
aikimarkCommented:
@pawnit

Did you mean to accept a solution to your problem?  Your last comment indicates that you do not have a properly working solution.
0
pawnitAuthor Commented:
Thanks for all your help guys! The error was on my part! I did mean to accept the accepted solution :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.