Reputation: 6697
How do I make my own word wrap function for strings? I want each line to be no longer than 50 characters and respect existing CRLFs.
Upvotes: 0
Views: 166
Reputation: 11519
Something like this, it will get you started (just a quick samle i mashed together):
Private Sub Doit()
Dim Source As String = ""
Source &= "How to make my own word wrap function for string, I want each line to be no longer than 50chars and take respect existing CRLFs" & vbCrLf & vbCrLf
Source &= "So this will be a new row."
Dim wrappedtext As String = wrap(Source, 20, vbNewLine)
MsgBox(wrappedtext)
End Sub
Function wrap(ByVal text As String, ByVal maxlength As Integer, ByVal newline As String) As String
Dim tmp() As String = Split(text.Replace(vbCrLf, " | "), " ")
Dim ret As String = ""
Dim wrk As String = ""
For Each word As String In tmp
If word = "|" Then
ret &= newline
wrk = ""
ElseIf word = "" Then
Else
If Len(wrk & word) <= maxlength Then
wrk &= " " & word
Else
ret &= wrk & newline
wrk = word & " "
End If
End If
Next
If wrk <> "" Then ret &= wrk
Return ret
End Function
Upvotes: 1
Reputation: 82
From which point of view? SW architecture?
Take a look at the decorator pattern. If you like to work with streams, in the book "Heads First: Design Patterns" a string modifier is proposed. It's in Java, but the general programming concept is described in a good way. Some pages are missing but you can find many infos here.
The algorithm itself is trivial, isn't it?
Upvotes: 0