fork(3) download
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8. public static string TrimLength(string text, int maxLength)
  9. {
  10. if (text.Length > maxLength)
  11. {
  12. maxLength -= "...".Length;
  13. maxLength = text.Length < maxLength ? text.Length : maxLength;
  14. bool isLastSpace = text[maxLength] == ' ';
  15. string part = text.Substring(0, maxLength);
  16. if (isLastSpace)
  17. return part + "...";
  18. int lastSpaceIndexBeforeMax = part.LastIndexOf(' ');
  19. if (lastSpaceIndexBeforeMax == -1)
  20. return part + "...";
  21. else
  22. return text.Substring(0, lastSpaceIndexBeforeMax) + "...";
  23. }
  24. else
  25. return text;
  26. }
  27.  
  28. public static void Main()
  29. {
  30. Console.WriteLine(TrimLength("What can UK learn from Spanish high speed rail when its crap", 49));
  31. Console.Write(TrimLength("What_can_UK_learn_from_Spanish_high_speed_rail_when its crap", 49));
  32. }
  33. }
Success #stdin #stdout 0.02s 33856KB
stdin
Standard input is empty
stdout
What can UK learn from Spanish high speed rail...
What_can_UK_learn_from_Spanish_high_speed_rail...