TrimStart is not trimming!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GrandpaB

    TrimStart is not trimming!

    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.ICompara ble.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.TrimStar t(trimChar)
    yTitle = Me.Title
    y1Title = yTitle.TrimStar t(trimChar)
    Return y1Title.Compare To(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
  • Rykie

    #2
    Re: TrimStart is not trimming!

    Have you tried simply to do a .Replace("""",' ')

    Comment

    • GrandpaB

      #3
      Re: TrimStart is not trimming!

      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

      Comment

      • Dennis

        #4
        Re: TrimStart is not trimming!

        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:
        [color=blue]
        > 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[/color]

        Comment

        • GrandpaB

          #5
          Re: TrimStart is not trimming!

          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.ICompara ble.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.TrimStar t(trimChar)
          'x1Title = xTitle.Replace( """", "") 'Suggestion Rykie
          yTitle = Me.Title
          y1Title = yTitle.TrimStar t(trimChar)
          'y1Title = yTitle.Replace( """", "") 'Suggestion Rykie
          Return y1Title.Compare To(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

          Comment

          • Rykie

            #6
            Re: TrimStart is not trimming!

            How about:

            if xTitle.StartsWi th("""") Then
            xTitle = xTitle.Substrin g(1)
            End If

            etc etc

            Comment

            • Rykie

              #7
              Re: TrimStart is not trimming!

              This worked for me too

              xTitle = xTitle.TrimStar t("""")

              Rykie

              Comment

              • GrandpaB

                #8
                Re: TrimStart is not trimming!

                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.TrimStar t(""""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.StartsWi th("""") Then
                x1Title = xTitle.Substrin g(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

                Comment

                Working...