开发者

ASP.NET C# Truncate

开发者 https://www.devze.com 2023-01-22 21:17 出处:网络
I have the following content stored inside a database column: <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut

I have the following content stored inside a database column:

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur ad开发者_JAVA技巧ipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

It is displayed using the following: <%= m.article %>

What I would like to do is strip out the <p> tags and then only show say 40 characters and end it with ... so that I would end up with something like:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat...

I remember reading that using Html.Encode would strip the HTML tags, but not 100% sure about that, and not sure how to combine this with a way of truncating the content to 40 characters. If someone could help me with this that'd be awesome. Thanks.


Off the top of my head, something like this?

public static string EncodeAndTrimText(this HtmlHelper helper, string text)
{
   string result = string.Empty;
   result = HttpUtility.HtmlEncode(result);
   if (result.length > 40)
      return result.Substring(0, 40) + "...";
   return result;
}

HttpUtility.HtmlEncode will only encode the <p> tag, not remove it. If want it removed totally, just do result = result.Replace("<p>", string.Empty);

Usage:

<% Html.EncodeAndTrimText(Model.SomeProperty) %>


<%= String.Format("{0:40}...", m.article.Replace("<p>", string.Empty).Replace("</p>", string.Empty) %>


I have some functions that do what you want. It uses regular expressions to strip the html. Here is an example program:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {    
            string s = @"<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>";    

            Console.WriteLine(Truncate(StripHTML(s), 40) + "...");
            Console.ReadLine();
        }    

        public static string StripHTML(string html)
        {
            return RegularExpressions.Regex.Replace(html, @"<.*?>", string.Empty, RegularExpressions.RegexOptions.IgnoreCase);    
        }

        public static string Truncate(string input, int length)
        {
            bool isTruncated = true;

            if (input.Length > length)
            {
                char[] TextEnds = { ' ', '\n', '\r', '\0', '\f', '\t', '\v' };
                string temp = input.Substring(0, length + 1);
                string temp2 = temp.TrimEnd(TextEnds);

                if (temp2 == temp)
                {
                    //we truncated in the middle of a word
                    temp2 = temp.Substring(0, temp.LastIndexOfAny(TextEnds));    
                }
                else
                {
                    //we did not truncate in the middle of a word
                    //now we just need to return temp2 

                    //we do need to determine if the actual text of the word 
                    //changed before we decide if we have really truncated the
                    if (temp2 == input.TrimEnd(TextEnds))
                        isTruncated = false;    
                }                    
                return temp2;
            }
            else
            {    
                return input;
            }    
        }    
    }
}


I accomplished this by adding a function to my Models class, example:

namespace InventorySystem.Models
{
    public class InventoryItem
    {
       public Int Id { get; set; }
       public string Notes { get; set; }
       ...
       public string NotesTruncated
       {
           get
           {
               //you could add some additional code here to remove the <p>
               return (Notes.Length > 50) ? Notes.Substring(0, 50) + "..." : Notes;
           }
       }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消