Paul T. Rykiel
Paul T. Rykiel

Reputation: 1227

Why does "string.Contains" not work

I am trying to find and replace the following string

<p><img width="560" height="207" src="~/media/1ECAC40BCE3C43CEA0FEDA423C1EF2D1.ashx" alt="Fifteen years of the NASDAQ" /></p>
<p><em>Source:&nbsp; Bloomberg, L.P.</em>&nbsp;</p>

Thus, I am trying to first find if the string contains "img"

and this is my code:

 // check for image width to change for mobile
                string gotit = "don't got it";
                string imgBody = Text.Render(Item, "Body");
                if (imgBody.ToLowerInvariant().Contains("<img width="))
                     gotit = "got it";

but it never changes gotit. What am I doing incorrectly?

Upvotes: 0

Views: 1207

Answers (1)

displayName
displayName

Reputation: 14389

Assuming that you are able to get the contents of html correctly from some magical method GetHTMLContents();...

var input = GetHTMLContents();
var gotIt = input.Contains("<img"); //if present, it will be true else false and more importantly, 'gotIt' is boolean
Console.WriteLine(gotIt);

Further, the magical method GetHTMLContents() can look like this:

//using System.Net;

using (WebClient client = new WebClient())
{
    string htmlCode = client.DownloadString("http://somesite.com/default.html");
}

Upvotes: 2

Related Questions