How can I remove the external <p> ... </p> from the line
I want to request a string (html) from a database and display it on a web page. The problem is that the data has
<p> around the text (ending with </p>
I want to disable this external tag in my viewmodel or controller model, which returns this data. What is the best way to do this in C #?
You may be redundant for your needs, but if you want to parse HTML, you can use HtmlAgilityPack - certainly a cleaner solution in general than most suggested here, although this may not be as significant:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("<p> around the text (ending with </p>");
string result = doc.DocumentNode.FirstChild.InnerHtml;
, , String.Substring, myString.Substring(3, myString.Length-7) .
, , , , HTML, BrokenGlass.
UPDATE: :
String filteredString = Regex.Match(myString, "^<p>(.*)</p>").ToString();
\s ^, . , , , <p>...</p>. .
If you are absolutely guaranteed that the string will always match the pattern <p>...</p>, then other solutions using data.Substring(3, data.Length - 6)are sufficient. If, however, it is likely that it might look different, then you really need to use an HTML parser. The consensus is that the HTML Agility Pack is the way to go.