Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[VB.NET]A word wrap function

I recently went looking for a good word wrap function that could
handle words that were more than one line long, or even longer than multiple lines. I found none, so I wrote this. It's solid and the code is documented, easy to read and easy to implement in your own applications.

Declarations:
Code
'none

Code:
Code
Public Function WrapText(ByVal Text As String, ByVal
LineLength As Integer) As List(Of String)

Dim ReturnValue As New List(Of String)
' Remove leading and trailing spaces
Text = Trim(Text)


Dim Words As String() = Text.Split(" ")

If Words.Length = 1 And Words(0).Length > LineLength
Then
' Text is just one big word that is longer than one line
' Split it mercilessly
Dim lines As Integer = (Int(Text.Length / LineLength)
+ 1)
Text = Text.PadRight(lines * LineLength)
For i = 0 To lines - 1
Dim SliceStart As Integer = i * LineLength
ReturnValue.Add(Text.Substring(SliceStart,
LineLength))
Next
Else
Dim CurrentLine As New System.Text.StringBuilder
For Each Word As String In Words
' will this word fit on the current line?
If CurrentLine.Length + Word.Length <
LineLength Then
CurrentLine.Append(Word & " ")
Else
' is the word too long for one line
If Word.Length > LineLength Then
' hack off the first piece, fill out the
current line and start a new line
Dim Slice As String =
Word.Substring(0, LineLength - CurrentLine.Length)
CurrentLine.Append(Slice)
ReturnValue.Add(CurrentLine.ToString)
CurrentLine = New
System.Text.StringBuilder

' Remove the first slice from the word
Word = Word.Substring(Slice.Length,
Word.Length - Slice.Length)

' How many more lines do we need for
this word?
Dim RemainingSlices As Integer =
Int(Word.Length / LineLength) + 1
For LineNumber = 1 To
RemainingSlices
If LineNumber = RemainingSlices
Then
'this is the last slice
CurrentLine.Append(Word & "
")
Else
' this is not the last slice
' hack off a slice that is one
line long, add that slice
' to the output as a line and
start a new line
Slice = Word.Substring(0,
LineLength)
CurrentLine.Append(Slice)

ReturnValue.Add(CurrentLine.ToString)
CurrentLine = New
System.Text.StringBuilder

' Remove the slice from the
word
Word =
Word.Substring(Slice.Length, Word.Length - Slice.Length)
End If
Next
Else
' finish the current line and start a new
one with the wrapped word
ReturnValue.Add(CurrentLine.ToString)
CurrentLine = New
System.Text.StringBuilder(Word & " ")
End If
End If
Next

' Write the last line to the output
If CurrentLine.Length > 0 Then
ReturnValue.Add(CurrentLine.ToString)
End If
End If
Return ReturnValue
End Function

orignaly written by: Duane Roelands


If You Like this tutorial,
please check out my other tutorials by clicking ==> here
PHP & .NET Contributor.
Thank for the Share Mate. Really Helpful and Useful.
(Apr 30, 2010 04:19 PM)TheBlackMarket Wrote:  Thank for the Share Mate. Really Helpful and Useful.

you are very welcome
PHP & .NET Contributor.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by FhackMar 2, 2022 03:52 AM 4 305 Mar 2, 2022 03:52 AM
Last Post: Fhack
[.NET] by FoursenalDec 6, 2014 06:23 PM 1 64 Dec 6, 2014 06:23 PM
Last Post: Corios
[.NET] by NielyMar 9, 2014 05:48 AM 10 186 Mar 9, 2014 05:48 AM
Last Post: Niely