Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2008

Tip: Looking for answers? Try searching our database.

splitting string with a string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CSharper - 30 Jul 2008 15:24 GMT
I have a html page that I retrived and stored in a string and I want
to split it based on <td> elements. 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] - 30 Jul 2008 15:27 GMT
> I have a html page that I retrived and stored in a string and I want
> to split it based on <td> elements. 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
CSharper - 30 Jul 2008 15:55 GMT
> > I have a html page that I retrived and stored in a string and I want
> > to split it based on <td> elements. I know only way you can split
[quoted text clipped - 5 lines]
>
> Jon

Duh???

Thanks.
Jon Skeet [C# MVP] - 30 Jul 2008 17:28 GMT
> > 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
Nicholas Paldino [.NET/C# MVP] - 30 Jul 2008 18:18 GMT
haha, he was talking about himself I believe.  As in "Duh, why didn't I
figure that out"

Signature

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

On Jul 30, 3:55 pm, CSharper <cshar...@gmx.com> wrote:
> > 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
Jon Skeet [C# MVP] - 30 Jul 2008 19:22 GMT
>     haha, he was talking about himself I believe.  As in "Duh, why didn't I
> figure that out"

Ah, that would explain it :)

Signature

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

Maxwell - 30 Jul 2008 20:28 GMT
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 ">".

On Jul 30, 3:55 pm, CSharper <cshar...@gmx.com> wrote:
> > 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
Pavel Minaev - 30 Jul 2008 15:29 GMT
> I have a html page that I retrived and stored in a string and I want
> to split it based on <td> elements. 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,
    StringSplitOptions options
)
Fredo - 30 Jul 2008 21:26 GMT
They may be using .NET 1.1, which doesn't have the string parameter
overloads.

On Jul 30, 6:24 pm, CSharper <cshar...@gmx.com> wrote:
> I have a html page that I retrived and stored in a string and I want
> to split it based on <td> elements. 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,
StringSplitOptions options
)
Nicholas Paldino [.NET/C# MVP] - 30 Jul 2008 15:37 GMT
CSharper,

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

Signature

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

>I have a html page that I retrived and stored in a string and I want
> to split it based on <td> elements. 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,
Göran Andersson - 30 Jul 2008 16:41 GMT
> I have a html page that I retrived and stored in a string and I want
> to split it based on <td> elements. 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.

Signature

Göran Andersson
_____
http://www.guffa.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.