The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
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.0KB

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