splitting string with a string

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

    splitting string with a string

    I have a html page that I retrived and stored in a string and I want
    to split it based on <tdelements. I know only way you can split
    using string.split is using characters. The other option is traverse
    and split. Is there any other way you can split a string using string
    token itself?
    Thanks,
  • Jon Skeet [C# MVP]

    #2
    Re: splitting string with a string

    On Jul 30, 3:24 pm, CSharper <cshar...@gmx.c omwrote:
    I have a html page that I retrived and stored in a string and I want
    to split it based on <tdelements. I know only way you can split
    using string.split is using characters. The other option is traverse
    and split. Is there any other way you can split a string using string
    token itself?
    Use Regex.Split.

    Jon

    Comment

    • Pavel Minaev

      #3
      Re: splitting string with a string

      On Jul 30, 6:24 pm, CSharper <cshar...@gmx.c omwrote:
      I have a html page that I retrived and stored in a string and I want
      to split it based on <tdelements. I know only way you can split
      using string.split is using characters. The other option is traverse
      and split. Is there any other way you can split a string using string
      token itself?
      Yes; use String.Split. It has an overload which takes String (not
      char) delimiters:

      public string[] Split(
      string[] separator,
      StringSplitOpti ons options
      )

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: splitting string with a string

        CSharper,

        Have you taken a look at the RegEx class? Specifically, the Split
        method on the RegEx class?


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "CSharper" <csharper@gmx.c omwrote in message
        news:57b26270-7f64-400c-b044-fe4a5e9d68af@r6 6g2000hsg.googl egroups.com...
        >I have a html page that I retrived and stored in a string and I want
        to split it based on <tdelements. I know only way you can split
        using string.split is using characters. The other option is traverse
        and split. Is there any other way you can split a string using string
        token itself?
        Thanks,

        Comment

        • CSharper

          #5
          Re: splitting string with a string

          On Jul 30, 9:27 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
          On Jul 30, 3:24 pm, CSharper <cshar...@gmx.c omwrote:
          >
          I have a html page that I retrived and stored in a string and I want
          to split it based on <tdelements. I know only way you can split
          using string.split is using characters. The other option is traverse
          and split. Is there any other way you can split a string using string
          token itself?
          >
          Use Regex.Split.
          >
          Jon
          Duh???

          Thanks.

          Comment

          • =?ISO-8859-1?Q?G=F6ran_Andersson?=

            #6
            Re: splitting string with a string

            CSharper wrote:
            I have a html page that I retrived and stored in a string and I want
            to split it based on <tdelements. I know only way you can split
            using string.split is using characters. The other option is traverse
            and split. Is there any other way you can split a string using string
            token itself?
            Thanks,
            As suggested, the Regex class also has a Split method, but you can do
            better than that with a regular expression.

            You can use the pattern "<td[^>]*>([\w\W]*?)</td>" with the Regex.Match
            method to find the contents of all td elements in the string.

            <td[^>]*matches the starting tag even if it has arguments
            [^>] matches any character except >
            * means zero or more matches
            () catches the value
            [\w\W] matches any character
            *? makes a non-gready match, so that it ends at the first </td>, not the
            last

            Note: This doesn't work well if you have nested tables.

            --
            Göran Andersson
            _____
            Göran Anderssons privata hemsida.

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: splitting string with a string

              On Jul 30, 3:55 pm, CSharper <cshar...@gmx.c omwrote:
              Use Regex.Split.
              >
              Duh???
              Which part didn't you understand? In the RegEx class, there's a Split
              method. Construct an appropriate regex, and call the Split method.

              As Pavel mentioned, String also now contains an overload for
              String.Split which takes an array of delimiter strings instead of
              chars. It's "new" to 2.0, but hopefully that won't be an issue for
              you.

              Jon

              Comment

              • Nicholas Paldino [.NET/C# MVP]

                #8
                Re: splitting string with a string

                haha, he was talking about himself I believe. As in "Duh, why didn't I
                figure that out"


                --
                - Nicholas Paldino [.NET/C# MVP]
                - mvp@spam.guard. caspershouse.co m

                "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                news:5bf9fb36-e3bf-4bcc-aa23-5a27d3a48b2a@s5 0g2000hsb.googl egroups.com...
                On Jul 30, 3:55 pm, CSharper <cshar...@gmx.c omwrote:
                Use Regex.Split.
                >
                Duh???
                Which part didn't you understand? In the RegEx class, there's a Split
                method. Construct an appropriate regex, and call the Split method.

                As Pavel mentioned, String also now contains an overload for
                String.Split which takes an array of delimiter strings instead of
                chars. It's "new" to 2.0, but hopefully that won't be an issue for
                you.

                Jon


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: splitting string with a string

                  Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c omwrote:
                  haha, he was talking about himself I believe. As in "Duh, why didn't I
                  figure that out"
                  Ah, that would explain it :)

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Web site: http://www.pobox.com/~skeet
                  Blog: http://www.msmvps.com/jon.skeet
                  C# in Depth: http://csharpindepth.com

                  Comment

                  • Maxwell

                    #10
                    Re: splitting string with a string

                    I actually used this functionality quite heavily recently, to narrow in on
                    an encoded url in a webpage source. I split the string after a "<td
                    id=\"...\">" element, or something similar, that occurred once and was
                    unique, and took the second part.
                    Then I took the first part of the split at "</td>".
                    Then I took the second part of "<a href=\"".
                    Then I took the first part of ">".

                    "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                    news:5bf9fb36-e3bf-4bcc-aa23-5a27d3a48b2a@s5 0g2000hsb.googl egroups.com...
                    On Jul 30, 3:55 pm, CSharper <cshar...@gmx.c omwrote:
                    Use Regex.Split.
                    >
                    Duh???
                    Which part didn't you understand? In the RegEx class, there's a Split
                    method. Construct an appropriate regex, and call the Split method.

                    As Pavel mentioned, String also now contains an overload for
                    String.Split which takes an array of delimiter strings instead of
                    chars. It's "new" to 2.0, but hopefully that won't be an issue for
                    you.

                    Jon


                    Comment

                    • Fredo

                      #11
                      Re: splitting string with a string

                      They may be using .NET 1.1, which doesn't have the string parameter
                      overloads.

                      "Pavel Minaev" <int19h@gmail.c omwrote in message
                      news:c90ccd06-c796-492d-9b5d-96bb72a3105f@34 g2000hsf.google groups.com...
                      On Jul 30, 6:24 pm, CSharper <cshar...@gmx.c omwrote:
                      I have a html page that I retrived and stored in a string and I want
                      to split it based on <tdelements. I know only way you can split
                      using string.split is using characters. The other option is traverse
                      and split. Is there any other way you can split a string using string
                      token itself?
                      Yes; use String.Split. It has an overload which takes String (not
                      char) delimiters:

                      public string[] Split(
                      string[] separator,
                      StringSplitOpti ons options
                      )


                      Comment

                      Working...