Posted By

ctrlbr34k on 02/23/12


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

BrockSamsom


Truncate Text for display


 / Published in: C#
 

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

  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  

You need to login to post a comment.