Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
A word wrap function for VB.NET
Author:
Duane Roelands
E-mail:
Click to e-mail author
Website:
http://www.GetQuitter.com
Submitted:
8/6/2009
Version:
VB 2008
Compatibility:
VB 2008
Category:
String Manipulation
Views:
24564
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:
'none
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
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement