locked
Remove HTML using substring RRS feed

  • Question

  • User-501297529 posted

    I want to remove the html that is stored in my database for Description field. Right now I'm able to shorten it less than 16 characters but when it is display on a datatable calling the ShortDescription field but the HTML still shows.

    <div>Test<spa ...

    I have it set up as this in my model class file:

    public string Description { get; set; }
    
    public string ShortDescription => Description?.Length > 16 ? $"{Description.Substring(0, 13)} ..." : Description;

    How do I fix this to strip out the html so it says "Test..."

    Friday, October 18, 2019 3:32 PM

Answers

  • User475983607 posted

    I would take advantage of HTML and JS to build this feature.  Wrap the content coming from the DB in another HTML tag and CSS class that shows 16 charterers.

    For Example.

    .ellipsis {
        display:inline-block;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 16ch;
        background-color:pink;
    }
        <div>
            <span class="ellipsis">
                I want to remove the html that is stored in my database for Description field. Right now I'm able
                to shorten it less than 16 characters but when it is display on a datatable calling the ShortDescription 
                field but the HTML still shows
            </span>
        </div>
    I want to remov...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 24, 2019 8:54 PM

All replies

  • User475983607 posted

    Unclear... My best guess is you are seeing HTML formatted text in the browser window.  The rest of my post is related to this assumption. 

    The MVC framework tries to protect you from malicious scripts by HTML encoding HTML characters.  You can easily see this affect when viewing the HTML source in Dev Tools (F12).  You'll see encoded characters like &lt; and &gt; of < and >.

    The @Html.Raw() method does not encode the string which allows you render raw HTML to the browser; https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-3.0#expression-encoding

    @Html.Raw("<span>Hello World</span>")

    The problem is your code striping then ending tag from the string.   

    public string ShortDescription => Description?.Length > 16 ? $"{Description.Substring(0, 13)} ..." : Description;

    If you use Html.Raw(), you'll have invalid HTML.  You'll need to come up with a different design.  Rather than shorting the string, render the string but use HTML, CSS and perhaps JS to affect the string in the browser.  For example, rather than substring, wrap the remaining string characters in an HTML tag that is not visible on screen.  sprinkle in a little JS or CSS to display the three dots. 

    Friday, October 18, 2019 4:32 PM
  • User1034446946 posted

    not tried it but what about:

    http://maxprog.net.pl/best-practice/solved-c-how-get-plain-text-from-html-string/

    or add aditional fields or information structures to deal with ui requirements.

    Friday, October 18, 2019 8:32 PM
  • User-501297529 posted

    Tried this

          <td>@Regex.Replace(dorItem.ShortDescription, @"<[^>]+>|&nbsp;", 
          "").Trim()</td>

    and it does remove most of html except

          <span style> 

    How can I get rid of that?

    Thursday, October 24, 2019 8:46 PM
  • User475983607 posted

    I would take advantage of HTML and JS to build this feature.  Wrap the content coming from the DB in another HTML tag and CSS class that shows 16 charterers.

    For Example.

    .ellipsis {
        display:inline-block;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 16ch;
        background-color:pink;
    }
        <div>
            <span class="ellipsis">
                I want to remove the html that is stored in my database for Description field. Right now I'm able
                to shorten it less than 16 characters but when it is display on a datatable calling the ShortDescription 
                field but the HTML still shows
            </span>
        </div>
    I want to remov...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 24, 2019 8:54 PM