471,330 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

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,
Jul 30 '08 #1
10 1271
On Jul 30, 3:24*pm, CSharper <cshar...@gmx.comwrote:
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
Jul 30 '08 #2
On Jul 30, 6:24*pm, CSharper <cshar...@gmx.comwrote:
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,
StringSplitOptions options
)

Jul 30 '08 #3
CSharper,

Have you taken a look at the RegEx class? Specifically, the Split
method on the RegEx class?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"CSharper" <cs******@gmx.comwrote in message
news:57**********************************@r66g2000 hsg.googlegroups.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,

Jul 30 '08 #4
On Jul 30, 9:27*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jul 30, 3:24*pm, CSharper <cshar...@gmx.comwrote:
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.
Jul 30 '08 #5
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
_____
http://www.guffa.com
Jul 30 '08 #6
On Jul 30, 3:55*pm, CSharper <cshar...@gmx.comwrote:
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

Jul 30 '08 #7
haha, he was talking about himself I believe. As in "Duh, why didn't I
figure that out"
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:5b**********************************@s50g2000 hsb.googlegroups.com...
On Jul 30, 3:55 pm, CSharper <cshar...@gmx.comwrote:
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
Jul 30 '08 #8
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 30 '08 #9
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]" <sk***@pobox.comwrote in message
news:5b**********************************@s50g2000 hsb.googlegroups.com...
On Jul 30, 3:55 pm, CSharper <cshar...@gmx.comwrote:
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
Jul 30 '08 #10
They may be using .NET 1.1, which doesn't have the string parameter
overloads.

"Pavel Minaev" <in****@gmail.comwrote in message
news:c9**********************************@34g2000h sf.googlegroups.com...
On Jul 30, 6:24 pm, CSharper <cshar...@gmx.comwrote:
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,
StringSplitOptions options
)
Jul 30 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by qwweeeit | last post: by
5 posts views Thread by fatted | last post: by
2 posts views Thread by Trint Smith | last post: by
20 posts views Thread by Opettaja | last post: by
2 posts views Thread by CharChabil | last post: by
6 posts views Thread by HMS Surprise | last post: by
2 posts views Thread by shadow_ | last post: by
4 posts views Thread by yogi_bear_79 | last post: by
37 posts views Thread by xyz | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.