Shorten text class


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

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


Copy this code and paste it in your HTML
  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


Comments


You need to login to view comments.