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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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
 
Jay said:
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
}
 
Back
Top