471,339 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Substract part of the string based on tags (c#)

Jay
Hi y'all,
I have a beginner question... I want to get the all the values inside of
two tags in a string.
for example:
string HTMLContent = "<b>this is a test</b> <!-- start --> Hello world <!--
End --> <i>anohter test</i> " ;

I want to get whatever is inside of the start and end tags:

string NewString = " Hello Word";

I will really apreciate any help. Thanks ahead,

Jay
Jul 21 '05 #1
3 4086
Dim st As String = "<b>this is a test</b> <!-- start --> Hello world <!-- >
End --> <i>anohter test</i> "
Dim r AS String
Dim intL As Integer = Len(st)
Dim intN, intP as Integer
If intL>0 Then
intN=Instr(0, "<!-- start --> ", st)
If intN >0 Then
Do While intP < intL
v= mid(IntP,1,st)
If v="<" then
Exit Do
End If
r=r & v
Loop
End If
End If


"Jay" <Ja*@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Hi y'all,
I have a beginner question... I want to get the all the values inside of
two tags in a string.
for example:
string HTMLContent = "<b>this is a test</b> <!-- start --> Hello world <!-- End --> <i>anohter test</i> " ;

I want to get whatever is inside of the start and end tags:

string NewString = " Hello Word";

I will really apreciate any help. Thanks ahead,

Jay

Jul 21 '05 #2
Jay
Thanks Doug,
Do you happen to have a c# version of this code?..if not, thats ok!
Again, thanks so much for the help.
~Jay

"Doug Versch" wrote:
Dim st As String = "<b>this is a test</b> <!-- start --> Hello world <!-- >
End --> <i>anohter test</i> "
Dim r AS String
Dim intL As Integer = Len(st)
Dim intN, intP as Integer
If intL>0 Then
intN=Instr(0, "<!-- start --> ", st)
If intN >0 Then
Do While intP < intL
v= mid(IntP,1,st)
If v="<" then
Exit Do
End If
r=r & v
Loop
End If
End If


"Jay" <Ja*@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Hi y'all,
I have a beginner question... I want to get the all the values inside of
two tags in a string.
for example:
string HTMLContent = "<b>this is a test</b> <!-- start --> Hello world

<!--
End --> <i>anohter test</i> " ;

I want to get whatever is inside of the start and end tags:

string NewString = " Hello Word";

I will really apreciate any help. Thanks ahead,

Jay


Jul 21 '05 #3
Jay <Ja*@discussions.microsoft.com> wrote:
Thanks Doug,
Do you happen to have a c# version of this code?..if not, thats ok!


Doug's code won't actually work. (It doesn't even compile without
modification, and needs further modification to work.)

I suggest you have a look at the answers to the same question you
posted a few days ago. Otherwise:

(with x as the string in question)

int startIndex = x.IndexOf ("<!-- start -->");
if (startIndex != -1)
{
int textStart = startIndex+14;
int endIndex = x.IndexOf("<!-- end -->", textStart);
if (endIndex != -1)
{
string result = x.Substring (startIndex, endIndex-startIndex);
}
else
{
// Whatever you want to do with no end tag
}
}
else
{
// Whatever you want to do with no start tag
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Hon Seng Phuah | last post: by
2 posts views Thread by Jay | last post: by
5 posts views Thread by Geoff | last post: by
9 posts views Thread by Francois Grieu | last post: by
reply views Thread by rosydwin | 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.