Posted By

Fogh on 08/28/10


Tagged

c text aspnet shorten


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tyster


Shorten text class


 / Published in: C#
 

Class to shorten text. For example for an article where people can click read more to read the whole article.

  1. public class CropText
  2. {
  3. public CropText()
  4. {
  5. }
  6.  
  7. public string ShortenText(string input, int MaxLenght, bool DoDots)
  8. {
  9. string result = input;
  10. int InputLength = input.Length;
  11.  
  12. string lastChar = result.Substring(result.Length - 1);
  13.  
  14. if (MaxLenght < InputLength)
  15. {
  16. result = input.Substring(0, MaxLenght);
  17.  
  18. if (lastChar == " ")
  19. {
  20. result = result.Substring(0, result.Length - 1);
  21. }
  22.  
  23. if (DoDots)
  24. {
  25. result += "...";
  26. }
  27. }
  28. return result;
  29. }
  30. }

Report this snippet  

You need to login to post a comment.