Click here to Skip to main content
14,930,183 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi All,

I just want to remove the before and after spaces from html string through regular expression.
My html string is just like

HTML
<div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>Hi This is lalit singh rathour</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div>

and 

<div><span><br></span></div><span><div><span><br></span></div>A&nbsp;</span>tightly coupling<span>, means where the one object/entity needs to have knowledge of other objects or we can say they depend largely on the interfaces where the service methods are declared. This can be avoided in small applications, but in large applications, these terms are required to be kept in mind else, it may lead to chaos.</span><div></div><div><span><br></span></div><div><span><br></span></div><div><span><br></span></div><div><span><br></span></div><div><span><br></span></div>


will you help how can i extract the html string without before and after spaces.

Thanks in advance!
Posted
Updated 8-Apr-15 5:19am
v2
Comments
Suvendu Shekhar Giri 8-Apr-15 2:59am
   
Where are those spaces exactly?
Tilak Rathour 8-Apr-15 3:50am
   
spaces that you seen in html <div><br></div><div><br></div><div>
Sinisa Hajnal 8-Apr-15 6:12am
   
Do you mean <br> tags?
Maciej Los 8-Apr-15 11:20am
   
What have you tried? Where are you stuck?

1 solution

If you mean spaces as in <br> tags always surrounded by <div> tags, you can simply try String's Replace method:
C#
String cleanedText = originalText.Replace("<div><br></br></div>","");


For more advanced matching you can turn to Regex.Replace, e.g:

C#
String cleanedTerxt = Regex.Replace(originalText, "<div>\s*<br>\s*</br></div>", "");

to also remove occurances where there is whitespace between the tags. Regex is a bit tricky to get right but you can for instance use https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx[^] as a reference.

Good luck.
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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