StringGenerator.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Azylee.Core.DataUtils.CollectionUtils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Azylee.Core.DataUtils.StringUtils
  7. {
  8. public class StringGenerator
  9. {
  10. /// <summary>
  11. /// 通过数字区间生成一组字符串(支持一个[-]区间)
  12. /// </summary>
  13. /// <param name="s"></param>
  14. /// <returns></returns>
  15. public static List<string> CreateByNumberSection(string s)
  16. {
  17. List<string> resultList = new List<string>();
  18. int startNumber = 0, stopNumber = 0, lens = 1;
  19. string startString = "", stopString = "", arrow = "<";
  20. string[] sp = s.Split('[', ']');
  21. foreach (string item in sp)
  22. {
  23. if (arrow == "-") arrow = ">";
  24. if (item.Contains("-"))
  25. {
  26. string[] numbers = ArrayTool.Formatter<string>(item.Split('-'), 2);
  27. if (int.TryParse(numbers[0], out startNumber) && int.TryParse(numbers[1], out stopNumber))
  28. {
  29. lens = numbers[0].Length;
  30. arrow = "-";
  31. }
  32. }
  33. if (arrow == "<") startString += item;
  34. if (arrow == ">") stopString += item;
  35. }
  36. if (startNumber < stopNumber)
  37. {
  38. for (int i = startNumber; i <= stopNumber; i++)
  39. {
  40. string number = i.ToString();
  41. if (number.Length < lens)
  42. {
  43. number = number.PadLeft(lens, '0');
  44. }
  45. resultList.Add(startString + number + stopString);
  46. }
  47. }
  48. if (!Ls.Ok(resultList)) resultList.Add(s);
  49. return resultList;
  50. }
  51. }
  52. }