Avatar of andy_entchev
andy_entchev
 asked on

Putting line breaks in a string if it's too long

Hello, experts!
I want to spread a string across multiple lines if it's longer than 40 characters.
In case the string is too long, the last word should have a vbCrLf inserted before it, so it appears on a new line. It's like what Word does when you're writing something and it won't fit on that line, so it brings the whole word on the next line.
As a final result, the string should have vbCrLf's inserted in the appropriate places.

So say the string was: "AAAAAA BBBBBBBB CCCCCC DDDDDDDDDDDDD"
The new string should be: "AAAAAA BBBBBBBB CCCCCC" & vbCrLf & "DDDDDDDDDDDDD"

How can I do this?
Visual Basic.NET

Avatar of undefined
Last Comment
andy_entchev

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ZeonFlash

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
jrscherer

Are you going to draw this text using Graphics.DwawString()? If yes, GDI+ can do the job for you.

    Dim txtRectangle As New RectangleF ' frame to draw text within
    ' set here the location and size of the rectangle

    Dim frm As New StringFormat ' use frm object to define alignment within rectangle,
                                                '  similar to a multi line text box
     frm.Alignment = StringAlignment.Near ' top alignment
     frm.LineAlignment = StringAlignment.Near ' left alignment

    ' this example works within a .paint event handler of a control or a form, using the e.Graphics argument
     e.Graphics.DrawString(Text, Font, New SolidBrush(ForeColor), txtRectangle, frm) ' here the line breaks
     ' within  the rectangle with the rules you have asked. The exception is that the size of the rectangle
     ' and the font in use will determine how many characters you will find on a single line.

If this is not what you need, go for a Regex guru. I am not

Jack.net



Jayadev Nair


Hi pal,

Try this.

Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
        ' the original String
        Dim str As String = txtString.Text
        ' Arraylist to keep the splitted String temporarily
        Dim splittedText As New System.Collections.ArrayList

        While Len(str) > 40
            ' find the last word split before length hits 40
            Dim FindLastSpaceToSplit As Integer = (40 - InStr(StrReverse(Mid(str, 1, 40)), " ")) + 1
            ' Add the specific spliited string to collection
            splittedText.Add(Mid(str, 1, FindLastSpaceToSplit))
            ' remove the spliited text from existing one
            str = Mid(str, FindLastSpaceToSplit)
        End While

        ' Join All Splitted one with Line Feed and set to textbox
        txtSplittedStr.Text = Join(splittedText.ToArray, vbCrLf)
    End Sub

Hope this would help you
Thanks
andy_entchev

ASKER
Thanks guys, your responses were very fast!
Unfortunately, jrscherer, I'm not drawing with GDI+.
compued, I tried your method but it seemed to be cutting off several sets of space-separeted words no matter what the string was.
ZeonFlash, your code worked perfectly! Thanks once again for the quick help, everyone!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23