TrimStart is not trimming!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am comparing two strings for sorting. In some cases the string may be
enclosed in quotes. Since the quote character is less than the A character,
all the strings enclosed in quotes will finish the sort ahead of all other
strings. My function first attempts to strip the leading quote character, if
it exists, from the string.

Code:
Public Function CompareTo(ByVal xArt As Object) As Integer _
Implements System.IComparable.CompareTo
'some Titles are enclosed in quotes; remove the quotes to sort
Dim xTitle As String
Dim x1Title As String
Dim yTitle As String
Dim y1Title As String
Dim trimChar() As Char = {""""c}
xTitle = CType(xArt, objArt).Title
x1Title = xTitle.TrimStart(trimChar)
yTitle = Me.Title
y1Title = yTitle.TrimStart(trimChar)
Return y1Title.CompareTo(x1Title)
End Function

I have added some dummy variables so that I could watch the TrimStart
process. xTitle and yTitle contain the pre-trimmed strings. x1Title and
y1Title should be the trimmed strings. However if xTitle = """Pansies"""
x1Title = """"Pansies""" The yTitle and y1Title variables have the same
problem!

I have created a short test program to trim quote characters; it works, but
something silly is eluding me in this function. Can you fine my error?

Thanks for your assistance.
 
Rykie,

Great suggestion! I tried .Replace as you suggested, but it did not work. I
got the same result; my puzzlement deepens. I think that I'm having a severe
attack of stupid and I'm missing something absolutely trival. Thanks for
your help.
 
Man, it looks like it ought to work. The only thing I can think of is to try

trimchar() as char = {chr(34)}
 
Dennis,

Another hearty thanks, but still no cigar! This is another good suggestion,
but it hasn't gotten to the root cause of the problem. I'm bamboozled and
bleary-eyed!

With all the suggestions my code now looks like this:
Public Function CompareTo(ByVal xArt As Object) As Integer _
Implements System.IComparable.CompareTo
'some Titles are enclosed in quotes; remove the quotes to sort
Dim xTitle As String
Dim x1Title As String
Dim yTitle As String
Dim y1Title As String
'Dim trimChar() As Char = {""""c}
Dim trimChar() As Char = {Chr(34)} 'Suggestion Dennis
xTitle = CType(xArt, objArt).Title
x1Title = xTitle.TrimStart(trimChar)
'x1Title = xTitle.Replace("""", "") 'Suggestion Rykie
yTitle = Me.Title
y1Title = yTitle.TrimStart(trimChar)
'y1Title = yTitle.Replace("""", "") 'Suggestion Rykie
Return y1Title.CompareTo(x1Title)
End Function


The list of Locals produces:
x1Title "“The Following Coldâ€" String
xTitle "“The Following Coldâ€" String
y1Title "“773 Stan Driveâ€" String
yTitle "“773 Stan Driveâ€" String
 
Rykie,

Problem solved, thanks to your suggestons. This proves that VB.Net is a
rich language and there are many ways to skin a cat, but your second
suggestion provided the clue on how to skin this one.

I am using Option Stirct so I had to implement your third suggestion as:

xTitle.TrimStart(""""c)

I introduced the extra variables, x1Title and y1Title, so that I could more
easily trace the progrm. Your second suggestion then became:

If xTitle.StartsWith("""") Then
x1Title = xTitle.Substring(1)
Else
x1Title = xTitle
End If

Stepping through your second suggestion showed that the StartsWith("""")
method never evaluated to true even when the string contained a leading quote
character.

I then inserted a MsgBox to display the ASCII code of the first character.
It was not a standard quote Char(34) it was a LEFT quote Char(147)!

As I previously said I had a severe attack of stupid and it was a trivial
problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top