StringExtendMethods.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace XYY.Core.Standard.ExtendMethods
  7. {
  8. public static class StringExtendMethods
  9. {
  10. public static string[] ToArray(this string str, int maxLength)
  11. {
  12. string[] arr = str.Split(' ', '.', ',');
  13. int page = ((str.Length + maxLength) - 1) / maxLength;
  14. string[] addressArr = new string[page];
  15. int flag = 0;
  16. int t = arr.Count();
  17. for (int i = 0; i < page; i++)
  18. {
  19. string temp = "";
  20. for (int j = 0; j < t; j++)
  21. {
  22. string splitor = j < t - 1 ? " " : String.Empty;//如果是最后一截,则不要加空格;
  23. if ((temp += arr[flag] + splitor).Length <= maxLength)
  24. {
  25. addressArr[i] += arr[flag] + splitor;
  26. flag++;
  27. }
  28. }
  29. t = arr.Count() - flag;
  30. }
  31. return addressArr;
  32. }
  33. }
  34. }