Posted By

corydeppen on 06/28/10


Tagged

c html-helper


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Vordreller
Tyster


Truncate text


 / Published in: C#
 

Simple HTML Helper which truncates a string to a given length.

  1. /// <summary>
  2. /// This is a simple HTML Helper which truncates a string to a given length
  3. /// </summary>
  4. /// <param name="helper">HTML Helper being extended</param>
  5. /// <param name="input">Input string to truncate</param>
  6. /// <param name="length">Max length of the string</param>
  7. /// <returns></returns>
  8. public static string Truncate(this HtmlHelper helper, string input, int length)
  9. {
  10. if (input.Length <= length)
  11. {
  12. return input;
  13. }
  14. else
  15. {
  16. return input.Substring(0, length) + "...";
  17. }
  18. }

Report this snippet  

You need to login to post a comment.