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(ds nHistory)
data = New DataSet
Dim dr As SqlClient.SqlDataReader
con = New SqlClient.SqlConnection(ds nHistory)
con.Open()
Dim c As New SqlClient.SqlCommand("SELE CT " _
& "DriverFirstName=RTrim(Fir stName), " _
& "DriverLastName=RTrim(Last Name), " _
& "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(CommandBeh avior.Sing leResult)
'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!
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(ds
data = New DataSet
Dim dr As SqlClient.SqlDataReader
con = New SqlClient.SqlConnection(ds
con.Open()
Dim c As New SqlClient.SqlCommand("SELE
& "DriverFirstName=RTrim(Fir
& "DriverLastName=RTrim(Last
& "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(CommandBeh
'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!
does it matter where in the string the letter T appears?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
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.