Reputation: 23
I have this paragraph inside my lotHtmlOriginal.Text
waters71 </strong></p>
<p><strong>websitesss.com/sblogin/login.shtml/</strong><br />
<strong>1234213:123123</strong><br />
<strong>213123:12312</strong><br />
<strong>4213:196028</strong><br />
<strong>32131:43423</strong><br />
<strong>4444444:96980507</strong></p>
<p><strong>htt
I issue is that when I make the second part of substring </p>
, the code fails because substring is not able to find it, but when I change it to certain things, it works.
It does not work when I change it too...
</strong>
This is my code
String St = lotHTMLOriginal.Text; ;
String firstPart = "blogin/login.shtml/</strong>";
String lastPart = "</p>";
int pFrom = St.IndexOf(firstPart) + firstPart.Length;
int pTo = St.IndexOf(lastPart);
String result = St.Substring(pFrom, pTo - pFrom);
lotHTML.Text = result;
And just in case it's not clear, I'm trying to get everything between blogin/login.shtml/</strong>
and </p>
Upvotes: 2
Views: 390
Reputation: 566
This code will find the first "</p>"
after the beginning of the firstPart.
String lastPart = "</p>";
int pFrom = St.IndexOf(firstPart) + firstPart.Length;
int pTo = 0;
while(pTo - pFrom < 0)
{
pTo = St.IndexOf(lastPart);
}
Upvotes: 0
Reputation: 32511
Using int pTo = St.IndexOf(lastPart);
you will get the index of the first </p>
, but you are looking for the last one so I think you should use
int pTo = St.LastIndexOf(lastPart);
OUTPUT:
<br /> <strong>1234213:123123</strong><br /> <strong>213123:12312</strong><br /> <strong>4213:196028</strong><br /> <strong>32131:43423</strong><br /> <strong>4444444:96980507</strong>
Upvotes: 0
Reputation: 10408
I believe this will fix the bug in your code. I've added a new variable to the mix. Let me know if this makes sense / helps.
String St = lotHTMLOriginal.Text; ;
String firstPart = "blogin/login.shtml/</strong>";
String lastPart = "</p>";
int pFrom = St.IndexOf(firstPart) + firstPart.Length;
// these three lines changed
string St_temp = St.Substring(pFrom);
int pTo = St_temp.IndexOf(lastPart);
String result = St.Substring(pFrom, pTo);
lotHTML.Text = result;
What I ultimately did was to trim the characters before firstPart
in St
to simplify the code somewhat. It limits the arithmetic you need to do to get the result you are looking for.
// this is the result
<br />
<strong>1234213:123123</strong><br />
<strong>213123:12312</strong><br />
<strong>4213:196028</strong><br />
<strong>32131:43423</strong><br />
<strong>4444444:96980507</strong>
Upvotes: 0
Reputation: 100630
It looks like you want to find particular string starting from previously found position like string.IndexOf:
int pTo = St.IndexOf(lastPart, pFrom);
Side note: if you need a lot of HTML processing it may be easier to use HTML parser (like HtmlAgilityPack) instead of using string matching or regular expressions, but for simple one-off code substring is ok.
Upvotes: 2