Link to home
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?
ASKER CERTIFIED SOLUTION
Avatar of ZeonFlash
ZeonFlash

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
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




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
Avatar of andy_entchev
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!