Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I have below html string that is generated in code behind :-

string html = "<div class="inner">
<div class="title">Under graduation</div>
<div class="detail"><span class="deg">B.Tech</span> 2017</div>
<div class="gr">technical university.</div>
</div>
<div class="inner">
<div class="title">Other Qualifications:</div>
<div class="cert">
<img src="https://abc.com/i/default_cert.png" onerror="this.onerror=null;this.src='https://abc.com/i/default_cert.png';" alt="1) Java Training from TRISECT, Noida. (2) Java Training from e-school, Bareilly. (3) BSNL" class="certificatelogo">
<div class="certificate__details certificate__details--bold">
<div class="certificate__details__heading"> 1) Java Training from TRISECT, Noida. (2) Java Training from e-school, Bareilly. (3) BSNL </div>
<div class="certificate__details__footer"> <span class="certificate-normal"> <em>•</em> ID: <span class="certifails-bold">3453454334</span> </span> </div>
</div>
</div>
</div>";

Now I want to remove tag from within above string.

Note - There May be multiple IMAGE TAGS in above string.

Can anybody help me on this?

Can we remove IMAGE TAGS by using regular expression in C#?

What I have tried:

I have tried using sub string. But position of image tags are not aligned.
Posted
Updated 8-Jul-21 8:57am
v2

The "What I have tried:" section is where you are meant to put the code that you have tried.
Quote:
I have tried using sub string. But position of image tags are not aligned.
I have no idea what you mean by "not aligned"

First you need to know where the img tag starts - that is simply a case of using the IndexOf [^] method.
Once you know where it starts, you can re-use an overload of IndexOf to find the end tag ">"
Then you can use those positions to remove the text between those points using overloads of Substring[^] to reconstruct the string from beginning of the string to the start of the first img tag combined with the part of the string that starts 1 character past the end of the img tag to the end of the string. E.g.
C#
int start = html.IndexOf(@"<img");
int stop = html.IndexOf(@">", start + 1);
html = string.Format("{0}{1}",html.Substring(0, start),html.Substring(stop +1))
Quote:
Note - There May be multiple IMAGE TAGS in above string.
Then put this into a loop that removes imgs until there are no more img tags - Hint - use while rather than do in case there are no imgs in the string when you start.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900