Match exact string using string.Contains() method in c#

c# html-agility-pack

Question

I have an html content. I'm parsing the content using HtmlAgilityPack. I Need to replace attribute
'align = "middle"' with 'align = "center"', I'm using function

if(htmlDoc.DocumentNode.OuterHttml.Contains("align = "middle""))
htmlDoc.DocumentNode.OuterHttml.Replace("align = "middle","align = "center"")

But if condition is returning true even for **valign = "middle"** !

What is that i need to put in if condition other than Contains()?

Accepted Answer

yes I'm trying to find match inside an html content.

Then use HtmlAgilityPack. Your code would be something like.

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(HtmlString);

var tds = doc.DocumentNode.SelectNodes("//td[@align='middle']");

or something like this using LINQ

var tds = doc.DocumentNode.Descendants("td")
                .Where(td => td.Attributes["align"].Value == "middle")
                .ToList();

Popular Answer

if your string is like "blablabla align = 'align = \"middle\" blablabla" then you can do:

Contains(" align = \"middle\" ") // spaces before and after

But how others says it's hard to understand what you want exactly.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why