Removing the trailing comma from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KashanKhan
    New Member
    • Oct 2007
    • 1

    #1

    Removing the trailing comma from a string

    hi there

    i have this string [code=vb]
    dim str as string

    str="1,2,3,4,"[/code]

    i required this
    [code=vb]
    str="1,2,3,4"[/code]

    can anyone help..!
    Last edited by pbmods; Oct 6 '07, 02:31 PM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Kashan. Welcome to TSDN!

    Please use CODE tags when posting source code:

    [CODE=vb]
    Visual Basic source code goes here.
    [/CODE]

    I'm going to go ahead and move this thread to the VB forum, where our resident Experts will be better able to help you out.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by KashanKhan
      i required this
      [code=vb]
      str="1,2,3,4"[/code]
      In what version of VB?

      Comment

      • jamesd0142
        Contributor
        • Sep 2007
        • 471

        #4
        Originally posted by Killer42
        In what version of VB?
        There is a substring method in VB.Net, or a mid function in VB.

        Either one of these will allow you to pick out characters at set positions and allow you to delete them.

        You could for example in VB.
        [code=vb]
        Dim str As String = "1,2,3,4,"
        Dim str1 As String

        Dim i As Integer = Len(str)
        str1 = Mid(str, 1, i - 1)
        [/code]

        This would pick out all characters except the last one which will be your comma, and store in str1.

        Thanks JAmes
        Last edited by Killer42; Oct 8 '07, 10:49 PM.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Thanks for that, James.

          However, if you're going to post code, please try to ensure it's working code.

          Dim str As String = "1,2,3,4,"
          ...is VB.Net syntax and won't work in VB6 or earlier. In those versions you would need to do something like...

          [CODE=vb]Dim str As String
          str = "1,2,3,4,"[/CODE]

          Comment

          • jamesd0142
            Contributor
            • Sep 2007
            • 471

            #6
            ahh i see, well thats a new 1 to me :P

            I only use vb.net and didnt realise the small differences.

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by jamesd0142
              ahh i see, well thats a new 1 to me :P

              I only use vb.net and didnt realise the small differences.
              :)
              That's probably just about the smallest of the differences. Some are huge.

              I downloaded VB 2005 Express Edition weeks ago. But I haven't done anything with it yet because I just don't know where to start. There's so much to learn.

              Comment

              • jamesd0142
                Contributor
                • Sep 2007
                • 471

                #8
                yes, maybe i should add that at home i use vb.net, and in work i downloaded the free vb 2005 express edition also, so im having to switch between the two! annoying!

                although when i finally tried to use this i find little differences.

                for example:

                dim a as string = "123" works for both.

                I understand this does'nt work for all versions of vb as you previously told me.

                some versions require this???

                dim a as string
                a = "123"

                Comment

                • ghendric1
                  New Member
                  • May 2022
                  • 1

                  #9
                  VB6 functions to trim characters

                  I know this post may be pretty old but I found myself working in old VB6 code once more and had to create a couple of functions that removes characters from the start and end of a string so I wrote these functions. Maybe someone will find these useful if they're working with VB6 again.

                  Code:
                  Public Function TrimEnd(ByVal someString As String, ByVal someChar As String) As String
                  
                      On Error GoTo Err_Proc
                      someString = Trim(someString)
                      someChar = Trim(someChar)
                      
                      If Len(someString) = 0 Then GoTo Exit_Proc
                      If Right(someString, Len(someChar)) <> someChar Then GoTo Exit_Proc
                  
                      While Right(someString, Len(someChar)) = someChar
                          someString = Left(someString, Len(someString) - Len(someChar))
                      Wend
                  
                  Exit_Proc:
                      TrimEnd = someString
                      Exit Function
                  
                  Err_Proc:
                  
                      'Err_Handler True, Err.Number, Err.Description, Erl, "Form1", "Function TrimEnd"
                      MsgBox Err.Description
                      Err.Clear
                      Resume Exit_Proc
                  
                  End Function
                  
                  Public Function TrimStart(ByVal someString As String, ByVal someChar As String) As String
                  
                      On Error GoTo Err_Proc
                      someString = Trim(someString)
                      someChar = Trim(someChar)
                  
                      If Len(someString) = 0 Then GoTo Exit_Proc
                      If Left(someString, Len(someChar)) <> someChar Then GoTo Exit_Proc
                  
                      While Left(someString, Len(someChar)) = someChar
                          someString = Mid(someString, Len(someChar) + 1)
                      Wend
                  
                  Exit_Proc:
                      TrimStart = someString
                      Exit Function
                  
                  Err_Proc:
                  
                      'Err_Handler True, Err.Number, Err.Description, Erl, "Form1", "Function TrimStart"
                      MsgBox Err.Description
                      Err.Clear
                      Resume Exit_Proc
                  End Function

                  Comment

                  Working...