how to remove last comma

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SuvarnaChaudhari
    New Member
    • Jan 2008
    • 14

    #1

    how to remove last comma

    Thanks lot!Its working,but I have again one doubt.I have created string but it looks like ('3435','4546', '546',).So plz tell me how to remove that last one comma,so that I can pass it thru sql query to show in grid.Thanking you!
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    you probably should generate that string in such a way that the comma is not there.
    BUT, if there will ALWAYS be an extra comma at the end, you can use .LastIndexOf() to find the last index of the comma, then use that index in .Remove()

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by SuvarnaChaudhar i
      Thanks lot!Its working,but I have again one doubt.I have created string but it looks like ('3435','4546', '546',).So plz tell me how to remove that last one comma,so that I can pass it thru sql query to show in grid.Thanking you!
      Why don't you post the code that you used to construct this string in the first place. That's what needs to be corrected.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Plater
        you probably should generate that string in such a way that the comma is not there.
        BUT, if there will ALWAYS be an extra comma at the end, you can use .LastIndexOf() to find the last index of the comma, then use that index in .Remove()
        Since when did you start typing faster than me?
        P.S Happy new year.

        Comment

        • chuckroc76
          New Member
          • Jan 2008
          • 2

          #5
          I use this all the time to build sql criterias. If you are doing what I think you are, there are 2 ways that I use to handle the building of the comma delimited string for a SQL clause.

          1) Basically, just always add a comma when adding an item to the comma delimited list. Then at the end, check to see if the last character is a comma and then remove it using
          myString = myString.SubStr ing(0,myString. Length-1);
          2) Before adding an item to the list, check to see if there are any items already in the list. If there are, then add a comma followed by the item you want to add to the list.

          Hope this helps.

          Comment

          • bennett
            New Member
            • Jul 2007
            • 12

            #6
            Or you could just use the trimend

            Code:
            Dim strVariable As String  = "1,2,3,4,5,6,"
            strVariable = strVariable.TrimEnd(",")
            Liam

            Comment

            Working...