Trimming a string

How would I trim a string that looks like this:

6004-6005_004.txt

Into:

6004-6005


Thanks.



Answer this question

Trimming a string

  • optyler

    How about a function such as this which will return the string to the left of a specific character.   So in your case you would call 

    Dim StrResult as string 
    StrResult = StripAtChar("333-000_004.txt", "_")

    And it would return "333-000" only.    This way it is not specific to length of strings or positions but to delimiting characters.

    Hope that helps....



        Private Function StripAtChar(ByVal x As String, ByVal Stripchar As Char) As String
            Try
                Dim pos As Integer
                pos = InStr(x, Stripchar)

                If pos > 0 Then
                    Return x.Substring(0, pos - 1)
                End If
            Catch ex As Exception

            End Try

        End Function


     


  • Paul200607

    variablename.remove(Character to start at, number of characters to delete) New variable as string.

    So I think your code should look something like
    NewString = OriginalString.remove(9, 4)

    But if you have trouble with the characters being selected just try playing around with those numbers.

  • Madcoder

    Private Function TrimAtChar(ByVal Instring As String, ByVal Char1 As char) As String

    On Error Resume Next

    TrimAtChar = Instring.Substring(0, Instring.IndexOf(Char1))

    End Function

    I just love the character extentions. I can't help it!



  • Tarek Ahmed Ismail

    That worked awesome!!!

    Thanks!!!!


  • Trimming a string