Extensions.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. namespace JiaZhiQuan.Common
  4. {
  5. public static class Extensions
  6. {
  7. private static Random rng = new Random();
  8. /// <summary>
  9. /// 随机排列
  10. /// </summary>
  11. public static void Shuffle<T>(this IList<T> list)
  12. {
  13. int n = list.Count;
  14. while (n > 1)
  15. {
  16. n--;
  17. int k = rng.Next(n + 1);
  18. T value = list[k];
  19. list[k] = list[n];
  20. list[n] = value;
  21. }
  22. }
  23. /// <summary>
  24. /// 如:通过110101,可获得 110101,1101,11
  25. /// </summary>
  26. /// <param name="relatedRegionCodes"></param>
  27. /// <returns></returns>
  28. public static List<string> Regions(this string relatedRegionCodes)
  29. {
  30. List<string> list = new List<string>();
  31. if (string.IsNullOrEmpty(relatedRegionCodes))
  32. {
  33. return list;
  34. }
  35. var codes = relatedRegionCodes.Split(',', StringSplitOptions.RemoveEmptyEntries);
  36. foreach (var code in codes)
  37. {
  38. if (code.Length == 6)
  39. {
  40. list.Add(code);
  41. list.Add(code.Substring(0, 4));
  42. list.Add(code.Substring(0, 2));
  43. }
  44. }
  45. return list;
  46. }
  47. public static byte[] FromBase64(this string base64WithoutPadding)
  48. {
  49. var base64 = base64WithoutPadding.Length % 4 == 0
  50. ? base64WithoutPadding
  51. : base64WithoutPadding + new string('=', 4 - base64WithoutPadding.Length % 4);
  52. return Convert.FromBase64String(base64.Replace("-", "+").Replace('_', '/'));
  53. }
  54. }
  55. }