Hack Forums
[.NET] [VB.NET]A word wrap function - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [VB.NET]A word wrap function (/showthread.php?tid=394061)



[VB.NET]A word wrap function - codepoet - Apr 30, 2010

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



* - TheBlackMarket - Apr 30, 2010

Thank for the Share Mate. Really Helpful and Useful.


* - codepoet - Apr 30, 2010

(Apr 30, 2010 04:19 PM)TheBlackMarket Wrote:  Thank for the Share Mate. Really Helpful and Useful.

you are very welcome