The next generation of the Teknik Services. Written in ASP.NET.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringExtensions.cs 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. namespace Teknik.Utilities
  7. {
  8. public static class StringExtensions
  9. {
  10. public static string Truncate(this string value, int maxLength)
  11. {
  12. if (string.IsNullOrEmpty(value)) return value;
  13. return value.Length <= maxLength ? value : value.Substring(0, maxLength);
  14. }
  15. public static string AddStringAtInterval(this string value, int interval, string insertStr)
  16. {
  17. if (interval <= 0 || value.Length < interval)
  18. return value;
  19. StringBuilder sb = new StringBuilder();
  20. int finalIndex = 0;
  21. for (int i = 0; i < value.Length; i = i + interval)
  22. {
  23. sb.Append(value.Substring(i, interval));
  24. sb.Append(insertStr);
  25. finalIndex = i;
  26. }
  27. if (finalIndex + interval != value.Length)
  28. {
  29. sb.Append(value.Substring(finalIndex, value.Length - finalIndex));
  30. }
  31. return sb.ToString();
  32. }
  33. }
  34. }