How can I take an < img tag from string? Sample string is given below. But too bad name and surname parts are dynamic and sometimes image names are numbers...
Lorem ipsum dolor sit amet <img src='http://www.mydomain.com/images/name_surname.jpg' /> Sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
What I've tried so far:
sMyText.Substring(sDescription.IndexOf("<img"), count?!);
how to count the who image character length? This is where I fail. Please help..
Use regular expressions.
string sMyText = "Lorem ipsum dolor sit amet <img src='http://www.mydomain.com/images/name_surname.jpg' /> Sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.";
Match match = Regex.Match(sMyText, "<img[^>]+>");
if (match.Success)
Console.WriteLine(match.Value);
I really don't like parsing html with a regex (see this question). I'd suggest using something like HtmlAgilityPack. It may seem overkill for your example but you'll save yourself a lot of pain in the future!
var document = new HtmlDocument();
document.LoadHtml(html);
var links = document.DocumentNode.Descendants("img");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With