By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

TrimStart is not trimming!

P: n/a
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.
--
GrandpaB
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Have you tried simply to do a .Replace("""",'')

Nov 21 '05 #2

P: n/a
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.
--
GrandpaB
Nov 21 '05 #3

P: n/a
Man, it looks like it ought to work. The only thing I can think of is to try

trimchar() as char = {chr(34)}
--
Dennis in Houston
"GrandpaB" wrote:
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.
--
GrandpaB

Nov 21 '05 #4

P: n/a
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

--
GrandpaB
Nov 21 '05 #5

P: n/a
How about:

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

etc etc

Nov 21 '05 #6

P: n/a
This worked for me too

xTitle = xTitle.TrimStart("""")

Rykie

Nov 21 '05 #7

P: n/a
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.
--
GrandpaB
Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.