Trim last "," delimiter of a string in VB.NET

gerfe picture gerfe · Apr 27, 2010 · Viewed 55k times · Source

This is my code:

With ad.Tables(2)
    For i As Integer = 0 To .Rows.Count - 1
        If .Rows(i)("name") & "" <> "" Then
            temp &= .Rows(i)("name") & ", "
        End If
    Next
End With
temp = temp.Trim(",")
testing &= "&Name=" & temp & vbCrLf

With this is get a comma in the end of the string. But if I do

temp = temp.Trim.Trim(",")

all commas are deleted.

How do I keep all commas and only delete the last one?

Answer

Captain America picture Captain America · Mar 20, 2014
temp = temp.TrimEnd(CChar(","))

That will do it and I think it is the easiest way.