Truncate Text for display


/ Published in: C#
Save to your folder(s)

Truncate("This is way too long", 11, "...") returns "This is way..."


Copy this code and paste it in your HTML
  1. public static string Truncate(this string myString, int limit, string symbol)
  2. {
  3. if (myString == null)
  4.      return null;
  5.  
  6.     if (limit < 0)
  7.      throw new ArgumentOutOfRangeException("limit", limit, "must be 0 or greater");
  8.  
  9.     if (symbol == null)
  10.      throw new ArgumentNullException("symbol must not be null");
  11.  
  12.     if (myString.Length < limit)
  13.         return myString;
  14.  
  15.     return myString.Substring(0, limit) + symbol;
  16. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.