Link to home
Start Free TrialLog in
Avatar of indy500fan
indy500fan

asked on

How do I trim a character off of a string if and only if it exists, once it's in a variable?

Friends,

I assign a string to a variable, from a qry, and if per chance the string contains the letter T, I want to strip that T off.  Is it possible?

Here is the code where it happens

Public Sub GetRaceResults(ByVal dsnHistory)

        Try
            Dim DriverFirstName As String
            Dim DriverLastName As String
            Dim CarNumber As String
            Dim RaceRank As Integer
            Dim Team As String

            Dim data As New DataSet
            Dim r As DataRow
         
            Dim con As New SqlClient.SqlConnection(dsnHistory)

           data = New DataSet

            Dim dr As SqlClient.SqlDataReader

            con = New SqlClient.SqlConnection(dsnHistory)

            con.Open()

            Dim c As New SqlClient.SqlCommand("SELECT " _
            & "DriverFirstName=RTrim(FirstName), " _
            & "DriverLastName=RTrim(LastName), " _
            & "CarNumber=RTrim([No]), " _
            & "RaceRank, " _
            & "Team=Rtrim(Team), " _
            & "License " _
            & "FROM " _
            & "Results " _
            & "WHERE (SeasonID = " & SeasonID & " AND EventID = " & EventID & "AND RunID = " & RunID & ")" _
            & "ORDER BY RaceRank", con)

            dr = c.ExecuteReader(CommandBehavior.SingleResult)

           'loop through the records
            While dr.Read()


                'assign DriverFirstName to variable
                DriverFirstName = dr.Item("DriverFirstName")
                'assign DriverLastName to variable
                DriverLastName = dr.Item("DriverLastName")
                CarNumber = dr.Item("CarNumber") <--If at this point, the string inside has a T on the end, can I kill that T?
                'assign RaceRank to variable
                RaceRank = dr.Item("RaceRank")
                'assign Team to variable
                Team = dr.Item("Team")

                ....

Thanks in advance!
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

does it matter where in the string the letter T appears?
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of indy500fan
indy500fan

ASKER

I think amit_g's close.  

Now, if there isn't a T on the end, will the code fail?

Let's say in scenario 1, CarNumber = "4T", amit_g's code should work.  But, if in scenario 2, CarNumber = "4", will there be any problems with:

CarNumber = CarNumber.TrimEnd("T"c)

Thanks!
It would not do anything. So $T would end up being 4 and 4 would also end up being 4.