Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 873
gerfe Avatar asked Apr 27 '10 19:04

gerfe


People also ask

How do I trim a character in a string?

There are three string methods that strip characters from a string: Trim() removes characters from both sides of the string. TrimStart() strips characters from the start of the string. TrimEnd() cuts away characters from the end of the string.

What is trim in VB net?

The Trim function removes spaces on both sides of a string. Tip: Also look at the LTrim and the RTrim functions.

How do you trim the last character of a string in VB net?

In c# or vb.net we can easily remove last character from string by using Remove or Trim or IndexOf properties.

How can I remove last 3 characters from a string in VB net?

To remove the last three characters from the string you can use string. Substring(Int32, Int32) and give it the starting index 0 and end index three less than the string length. It will get the substring before last three characters.


2 Answers

temp = temp.TrimEnd(CChar(","))

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

like image 87
Captain America Avatar answered Oct 14 '22 20:10

Captain America


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

or

temp = temp.Trim().Remove(temp.Length - 1)
like image 27
Hans Olsson Avatar answered Oct 14 '22 20:10

Hans Olsson