Love this little trick with the StringBuilder
Oct 25th, 2005 by jbloom
If you are concatenating a list of items and adding a spacer between them like a , or ; you’ll need to make sure the last entry doesn’t have that separator.
So you can do something complicated like figuring out on each cycle if this the last item and not adding it or you can use this little trick with the StringBuilder.
Public Function ArrayListToString(ByVal list As ArrayList) As String
Dim sb As New StringBuilder
Dim val As String
For Each val In list
sb.Append(val & “ , ”)
Next
‘This is the Trick, your changing the length of the StringBuilder
sb.Length = sb.Length - 3
End If
End Function
Dim sb As New StringBuilder
Dim val As String
For Each val In list
sb.Append(val & “ , ”)
Next
If
sb.ToString.EndsWith(“ , ”) Then‘This is the Trick, your changing the length of the StringBuilder
sb.Length = sb.Length - 3
End If
Return
sb.ToStringEnd Function
Colorized by: CarlosAg.CodeColorizer