TextBuilder
Public Structure TextBuilder
Private text As String
Public Sub Add(value As String)
Add(value, "")
End Sub
Public Sub Add(values As IEnumerable(Of String))
For Each value As String In values
If Not value Is Nothing Then
Me.Add(value)
End If
Next
End Sub
Public Sub Add(value As String, separator As String)
Me.text += value.Aggregate(" "c, Function(s As String, current As String)
If Not String.IsNullOrEmpty(s) Then
Return String.Format("{0}{1}{2}", s, separator, current)
End If
Return String.Format("{0}", current)
End Function)
End Sub
Private Function Trim(s As String, separator As Char) As String
While s.Length > 0 AndAlso s(0) = separator
s = s.Substring(1)
End While
Return s
End Function
Public Function TryParse(value As String, ByRef builder As TextBuilder) As Boolean
Try
builder = New TextBuilder
builder.Add(value)
Return True
Catch ex As Exception
End Try
builder = Nothing
Return False
End Function
Public Overrides Function ToString() As String
Dim s As String = text
If Not String.IsNullOrEmpty(s) Then
Return Trim(s, " "c)
End If
Return s
End Function
End Structure
Usage:
' Usage (1)
Dim builder As New TextBuilder
builder.Add("omar")
builder.Add("amin")
Dim result As String = builder.ToString
' Usage (2)
Dim strings As String() = New String() {"Omar", "amin"}
Dim tb As New TextBuilder
tb.Add(strings)
Dim result As String = builder.ToString
' Usage (3)
Dim txtbuilder As TextBuilder = Nothing
txtbuilder.TryParse(Nothing, txtbuilder)
Dim result As String = txtbuilder.ToString
Private text As String
Public Sub Add(value As String)
Add(value, "")
End Sub
Public Sub Add(values As IEnumerable(Of String))
For Each value As String In values
If Not value Is Nothing Then
Me.Add(value)
End If
Next
End Sub
Public Sub Add(value As String, separator As String)
Me.text += value.Aggregate(" "c, Function(s As String, current As String)
If Not String.IsNullOrEmpty(s) Then
Return String.Format("{0}{1}{2}", s, separator, current)
End If
Return String.Format("{0}", current)
End Function)
End Sub
Private Function Trim(s As String, separator As Char) As String
While s.Length > 0 AndAlso s(0) = separator
s = s.Substring(1)
End While
Return s
End Function
Public Function TryParse(value As String, ByRef builder As TextBuilder) As Boolean
Try
builder = New TextBuilder
builder.Add(value)
Return True
Catch ex As Exception
End Try
builder = Nothing
Return False
End Function
Public Overrides Function ToString() As String
Dim s As String = text
If Not String.IsNullOrEmpty(s) Then
Return Trim(s, " "c)
End If
Return s
End Function
End Structure
Usage:
' Usage (1)
Dim builder As New TextBuilder
builder.Add("omar")
builder.Add("amin")
Dim result As String = builder.ToString
' Usage (2)
Dim strings As String() = New String() {"Omar", "amin"}
Dim tb As New TextBuilder
tb.Add(strings)
Dim result As String = builder.ToString
' Usage (3)
Dim txtbuilder As TextBuilder = Nothing
txtbuilder.TryParse(Nothing, txtbuilder)
Dim result As String = txtbuilder.ToString
Comments
Post a Comment