如何为字符串创建自己的自动换行功能?我希望每行不超过50个字符并尊重现有的CRLF。
答案 0 :(得分:1)
像这样的东西,它会让你开始(只是我一起捣碎的快速小样):
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
答案 1 :(得分:0)
从哪个角度来看? SW架构?
看一下装饰模式。如果您喜欢使用流,请在“Heads First:Design Patterns”一书中提出一个字符串修饰符。它是用Java编写的,但是通用编程概念的描述很好。有些页面丢失,但您可以找到许多信息here。
算法本身很简单,不是吗?