• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 885
  • Last Modified:

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
  • 2
1 Solution
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.
Inquisitive000Author Commented:
this program truncates html string efficiently.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now