Search for Quote inside a string

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

    Search for Quote inside a string

    Hi,

    completeString = "<p><a href="http://www.google.com" > ..... "

    Now I want to extract the link pointed out by href so I try to find the
    position of first the href tag and then try to see where the quotes begin.

    pos_tag = completeString. IndexOf("href", 0, completeString. Length);

    pos_text_tag_st art = completeString. IndexOf("\"", pos_tag,
    completeString. Length);

    but this last line gives me an error - how can I find the position of a
    quote - " in a string?

    I have also tried :

    pos_text_tag_st art = completeString. IndexOf(""", pos_tag,
    completeString. Length);



    Any help is appreciated.


  • Rob Windsor [MVP]

    #2
    Re: Search for Quote inside a string

    I think all of these will work:

    """" (four double quotes)
    '"' (single quote, double quote, single quote)
    Convert.ToChar( 32)
    (char)32

    --
    Rob Windsor [MVP-VB]
    G6 Consulting
    Toronto, Canada


    "temp" <temporary_immo rtality@yahoo.c om> wrote in message
    news:upLReQaEGH A.3984@TK2MSFTN GP14.phx.gbl...[color=blue]
    > Hi,
    >
    > completeString = "<p><a href="http://www.google.com" > ..... "
    >
    > Now I want to extract the link pointed out by href so I try to find the
    > position of first the href tag and then try to see where the quotes begin.
    >
    > pos_tag = completeString. IndexOf("href", 0, completeString. Length);
    >
    > pos_text_tag_st art = completeString. IndexOf("\"", pos_tag,
    > completeString. Length);
    >
    > but this last line gives me an error - how can I find the position of a
    > quote - " in a string?
    >
    > I have also tried :
    >
    > pos_text_tag_st art = completeString. IndexOf(""", pos_tag,
    > completeString. Length);
    >
    >
    >
    > Any help is appreciated.
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Search for Quote inside a string

      Rob Windsor [MVP] <rob.windsor.no .spam@gmail.com > wrote:[color=blue]
      > I think all of these will work:
      >
      > """" (four double quotes)
      > '"' (single quote, double quote, single quote)[/color]

      These two won't. You need:
      @""""
      "\""
      or '\"'.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • temp

        #4
        Re: Search for Quote inside a string


        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
        news:MPG.1e26ad b739c5f1fc98cc7 0@msnews.micros oft.com...[color=blue]
        > Rob Windsor [MVP] <rob.windsor.no .spam@gmail.com > wrote:[color=green]
        >> I think all of these will work:
        >>
        >> """" (four double quotes)
        >> '"' (single quote, double quote, single quote)[/color]
        >
        > These two won't. You need:
        > @""""
        > "\""[/color]

        Doing "\"" doesnt work - see output from immediate window while debugging:


        ompleteString.I ndexOf("\"", pos_tag, completeString. Length)

        'completeString .IndexOf("\"", pos_tag, completeString. Length)' threw an
        exception of type 'System.Argumen tOutOfRangeExce ption'

        base {System.Argumen tException}: {"Count must be positive and count must
        refer to a location within the string/array/collection.\r\n Parameter name:
        count"}

        ActualValue: null

        Message: "Count must be positive and count must refer to a location within
        the string/array/collection.\r\n Parameter name: count"



        [color=blue]
        > or '\"'.
        >[/color]

        Comment

        • Mike Schilling

          #5
          Re: Search for Quote inside a string


          "temp" <temporary_immo rtality@yahoo.c om> wrote in message
          news:e8YaZpbEGH A.3004@TK2MSFTN GP15.phx.gbl...[color=blue]
          >
          > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          > news:MPG.1e26ad b739c5f1fc98cc7 0@msnews.micros oft.com...[color=green]
          >> Rob Windsor [MVP] <rob.windsor.no .spam@gmail.com > wrote:[color=darkred]
          >>> I think all of these will work:
          >>>
          >>> """" (four double quotes)
          >>> '"' (single quote, double quote, single quote)[/color]
          >>
          >> These two won't. You need:
          >> @""""
          >> "\""[/color]
          >
          > Doing "\"" doesnt work - see output from immediate window while debugging:
          >
          >
          > ompleteString.I ndexOf("\"", pos_tag, completeString. Length)
          >
          > 'completeString .IndexOf("\"", pos_tag, completeString. Length)' threw an
          > exception of type 'System.Argumen tOutOfRangeExce ption'
          >
          > base {System.Argumen tException}: {"Count must be positive and count must
          > refer to a location within the string/array/collection.\r\n Parameter name:
          > count"}
          >
          > ActualValue: null
          >
          > Message: "Count must be positive and count must refer to a location within
          > the string/array/collection.\r\n Parameter name: count"[/color]

          The problem is with the count, not with the character you're looking for.
          You're trying to look from pos_tag to pos_tag + Length -1, which is beyond
          the end of the string. Since you want to search to the end of the entire,
          just use the two-argument form:

          :pos_tag = 'completeString .IndexOf("\"", pos_tag);

          [color=blue]
          >
          >
          >
          >[color=green]
          >> or '\"'.
          >>[/color]
          >[/color]


          Comment

          • ray_f

            #6
            RE: Search for Quote inside a string

            I believe regular expression would make things easier in your case.

            Try something like this:
            (?<Protocol>htt p):\/\/(?<Domain>[\w.]+\/?)\S*(?x)

            And then you can parse the input string with
            System.Text.Reg ularExpressions .Regex, which will probably save you tens of
            lines of code.

            A great tools called Expresso from Ultrapico can help you build a regular
            expression visually. Worth trying in case you are not familiar with regular
            expression.

            Good luck!


            "temp" wrote:
            [color=blue]
            > Hi,
            >
            > completeString = "<p><a href="http://www.google.com" > ..... "
            >
            > Now I want to extract the link pointed out by href so I try to find the
            > position of first the href tag and then try to see where the quotes begin.
            >
            > pos_tag = completeString. IndexOf("href", 0, completeString. Length);
            >
            > pos_text_tag_st art = completeString. IndexOf("\"", pos_tag,
            > completeString. Length);
            >
            > but this last line gives me an error - how can I find the position of a
            > quote - " in a string?
            >
            > I have also tried :
            >
            > pos_text_tag_st art = completeString. IndexOf(""", pos_tag,
            > completeString. Length);
            >
            >
            >
            > Any help is appreciated.
            >
            >
            >[/color]

            Comment

            • Ignacio Machin \( .NET/ C# MVP \)

              #7
              Re: Search for Quote inside a string

              Hi,
              [color=blue]
              >
              > ompleteString.I ndexOf("\"", pos_tag, completeString. Length)
              >
              > 'completeString .IndexOf("\"", pos_tag, completeString. Length)' threw an
              > exception of type 'System.Argumen tOutOfRangeExce ption'
              >
              > base {System.Argumen tException}: {"Count must be positive and count must
              > refer to a location within the string/array/collection.\r\n Parameter name:
              > count"}[/color]

              Well, the error is pretty self explanatory, pos_tag is probably > 0
              therefore pos_tag + string.Length > string.Length , therefore you get the
              error, it's CLEARLY stated in MSDN !

              Just do IndexOf( "\"" , pos_tag );

              Now , I STRONGLY suggest you to take the advice of Ray and use a regular
              expression instead, you can find the url enclosed in " or in ' or even
              nothing.
              To further complicate the things, you could have things like : <a href="#"
              onclick="window .open('url');re turn false">

              Have fun! :)


              --
              Ignacio Machin,
              ignacio.machin AT dot.state.fl.us
              Florida Department Of Transportation


              Comment

              • temp

                #8
                Re: Search for Quote inside a string


                "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
                in message news:u1ybGAgEGH A.336@TK2MSFTNG P14.phx.gbl...[color=blue]
                > Hi,[/color]

                Hi

                [color=blue]
                > Now , I STRONGLY suggest you to take the advice of Ray and use a regular
                > expression instead, you can find the url enclosed in " or in ' or even
                > nothing.
                > To further complicate the things, you could have things like : <a
                > href="#" onclick="window .open('url');re turn false">[/color]


                Did I miss something? I cannot see the post of Ray where he has suggested
                regular expressions?


                [color=blue]
                > Have fun! :)
                >
                >
                > --
                > Ignacio Machin,
                > ignacio.machin AT dot.state.fl.us
                > Florida Department Of Transportation
                >[/color]


                Comment

                • temp

                  #9
                  IGORE ABOVE POST

                  Found Ray's suggestion (strange that I still dont see it in my news client,
                  but I see his post on a different site).



                  "temp" <temporary_immo rtality@yahoo.c om> wrote in message
                  news:%23IulHCkE GHA.336@TK2MSFT NGP14.phx.gbl.. .[color=blue]
                  >
                  > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
                  > wrote in message news:u1ybGAgEGH A.336@TK2MSFTNG P14.phx.gbl...[color=green]
                  >> Hi,[/color]
                  >
                  > Hi
                  >
                  >[color=green]
                  >> Now , I STRONGLY suggest you to take the advice of Ray and use a regular
                  >> expression instead, you can find the url enclosed in " or in ' or even
                  >> nothing.
                  >> To further complicate the things, you could have things like : <a
                  >> href="#" onclick="window .open('url');re turn false">[/color]
                  >
                  >
                  > Did I miss something? I cannot see the post of Ray where he has suggested
                  > regular expressions?
                  >
                  >
                  >[color=green]
                  >> Have fun! :)
                  >>
                  >>
                  >> --
                  >> Ignacio Machin,
                  >> ignacio.machin AT dot.state.fl.us
                  >> Florida Department Of Transportation
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  Working...