How would I split Html from a string and set a character limit?

I get a string from a list of items. The string is currently displayed as "item.ItemDescription" (9th line below)

I want to remove all html from this line. And set the character limit to 250 after removing the html. Is there an easy way to do this? I saw that there are messages that say to install the Agility Pack, but I was looking for something simpler.

EDIT:
It does not always contain html. If the client wanted to add a bold or italic tag to the item name in the description, it will be displayed as <"strong"> Item Name <"/ strong">, for example, I want to delete all html regardless of what is entered.

<tbody> @foreach (var itemin Model.itemList) { <tr id="@("__filterItem_" + item.EntityId + "_" + item.EntityTypeId)"> <td> @Html.ActionLink(item.ItemName, "Details", "Item", new { id = item.EntityId }, null) </td> <td> item.ItemDescription </td> <td> @if (Model.IsOwner) { <a class="btnDelete" title="Delete" itemid="@(item.EntityId)" entitytype="@item.EntityTypeId" filterid="@Model.Id">Delete</a> } </td> </tr> } </tbody> 
+5
source share
3 answers

This Regex will select any html tags (including those with double quotes, such as <"strong">:

 <[^>]*> 

Take a look here: http://regexr.com/3cge4

Using C # Regular Expressions to Remove HTML Tags

From there, you can simply check the size of the string and display it accordingly.

 var itemDescriptionStripped = Regex.Replace(item.ItemDescription, @"<[^>]*>", String.Empty); if (itemDescriptionStripped.Length >= 250) itemDescriptionStripped.Substring(0,249); else itemDescriptionStripped; 
0
source

Your best IMO option is night to sort out the parsing nightmare with all the possible values, why not just insert class=someCssClassName in the <td> as an attribute. Then control the length color using CSS.

An even better idea is to assign a class containing <tr class=trClass> , and then apply CSS to the long <td> children.

0
source

You can do something like this to remove all tags (opening, closing, and self-closing) from the string, but this can have unintended consequences of removing user-entered things that were not supposed to be html tags:

 text = Regex.Replace(text, "<\/?[^>]*\/?>", String.Empty); 

Instead, I would recommend something like this and make it clear to the user that html is not supported:

 text = text.Replace("<", "&lt;"); text = text.Replace(">", "&gt;"); 

Remember to check the 250 character limit before conversion:

 text = text.Substring(0, 250); 
0
source

Source: https://habr.com/ru/post/1239524/


All Articles