Posted By

krisdb on 07/18/08


Tagged

c-sharp


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

umang_nine
ddevening


Truncate


 / Published in: C#
 

  1. public static string TruncateChar(string strText, int intCharCount)
  2. {
  3. string strReturn = "";
  4.  
  5. try
  6. {
  7. if (strText.Length > intCharCount)
  8. strReturn = strText.Substring(0, intCharCount) + "...";
  9. else
  10. strReturn = strText;
  11. }
  12. catch { }
  13. return strReturn;
  14. }
  15.  
  16.  
  17. public static string TruncateWords(string strText, int intWordCount)
  18. {
  19. string strReturn = "";
  20.  
  21. try
  22. {
  23. string[] strWords = strText.Split(' ');
  24.  
  25. if (strWords.Length < intWordCount)
  26. intWordCount = strWords.Length;
  27.  
  28. for (int x = 0; x <= intWordCount; x++)
  29. strReturn += strWords[x] + " ";
  30.  
  31. if (strWords.Length > intWordCount)
  32. strReturn = strReturn.Trim() + "...";
  33. }
  34.  
  35. catch { /* do nothing */ }
  36.  
  37. return strReturn;
  38.  
  39. }

Report this snippet  

You need to login to post a comment.