views:

271

answers:

2

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 this

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

all commas are deleted. How do i keep all commas and only delete the last one?

+1  A: 
temp = temp.Trim().Substring(0, temp.Length - 1)

or

temp = temp.Trim().Remove(temp.Length - 1)
ho1
thanks, worked perfect
gerfe
+1  A: 

You can avoid the Trim/extra character if you set a delimiter within the loop

Dim delimiter as string = ""
For i As Integer = 0 To .Rows.Count - 1
   If .Rows(i)("name") & "" <> "" Then
      temp &= delimiter & .Rows(i)("name")
      delimiter = ","
   End If
Next
Alex K.
I removed the If check from your post because it is actually slower. The runtime is smart enough to optimize away the assignment like this, but it has to evaluate the if condition on every iteration.
Joel Coehoorn
Ah, nice tip, thanks
Alex K.