Split Html String in c# while preserving format (inserting missing tags)

Hi ,

I am trying to split an html string into two substrings of particular length in C#. In the process the start and end html tags are missing in the substrings. I want to append those missing tags in substrings to preserve the format of the text. Any help is greatly appreciated.

For eg
string testHTML = "<p> This is a <b><u>html test</u></b> string.</p>"

if i want to split this as "<p> This is a <b><u>html " and "test</u></b> string.</p>". the format will be lost if I display in two separate labels. Please help..

Thanks in Advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roopesh ReddyIT AnalystCommented:

Obviously! Since the HTML string is not complete. You are plucking the HTML part which doesn't have proper ending!
Gary DavisDir Internet SvcsCommented:
This is not easy to do but here is one possibility. Create both strings as copies of the full string

"<p> This is a <b><u>html test</u></b> string.</p>"
"<p> This is a <b><u>html test</u></b> string.</p>"

Then for the first string, remove all text between tags on the first half and then remove all text between tags on the 2nd half. You will wind up with some empty tags but that is ok.

"<p> This is a <b><u>html </u></b></p>"
"<p><b><u>test</u></b> string.</p>"

It is easier to identify text outside tags than try to interpret all the tags and their open/close state.

Gary Davis
Inquisitive000Author Commented:
resolved by me.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Inquisitive000Author Commented:
this program truncates html string efficiently.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.