CommonUtil.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace WMS.Util
  8. {
  9. /// <summary>
  10. /// 描 述:常用公共类
  11. /// </summary>
  12. public static class CommonUtil
  13. {
  14. #region Stopwatch计时器
  15. /// <summary>
  16. /// 计时器开始
  17. /// </summary>
  18. /// <returns></returns>
  19. public static Stopwatch TimerStart()
  20. {
  21. Stopwatch watch = new Stopwatch();
  22. watch.Reset();
  23. watch.Start();
  24. return watch;
  25. }
  26. /// <summary>
  27. /// 计时器结束
  28. /// </summary>
  29. /// <param name="watch">Stopwatch</param>
  30. /// <returns></returns>
  31. public static double TimerEnd(Stopwatch watch)
  32. {
  33. watch.Stop();
  34. return watch.ElapsedMilliseconds;
  35. }
  36. #endregion
  37. #region 删除数组中的重复项
  38. /// <summary>
  39. /// 删除数组中的重复项
  40. /// </summary>
  41. /// <param name="values">重复值</param>
  42. /// <returns></returns>
  43. public static string[] RemoveDup(string[] values)
  44. {
  45. List<string> list = new List<string>();
  46. for (int i = 0; i < values.Length; i++)//遍历数组成员
  47. {
  48. if (!list.Contains(values[i]))
  49. {
  50. list.Add(values[i]);
  51. };
  52. }
  53. return list.ToArray();
  54. }
  55. #endregion
  56. #region 自动生成日期编号
  57. /// <summary>
  58. /// 自动生成编号 201008251145409865
  59. /// </summary>
  60. /// <returns></returns>
  61. public static string CreateNo()
  62. {
  63. Random random = new Random();
  64. string strRandom = random.Next(1000, 10000).ToString(); //生成编号
  65. string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如
  66. return code;
  67. }
  68. #endregion
  69. #region 生成0-9随机数
  70. /// <summary>
  71. /// 生成0-9随机数
  72. /// </summary>
  73. /// <param name="codeNum">生成长度</param>
  74. /// <returns></returns>
  75. public static string RndNum(int codeNum)
  76. {
  77. StringBuilder sb = new StringBuilder(codeNum);
  78. Random rand = new Random();
  79. for (int i = 1; i < codeNum + 1; i++)
  80. {
  81. int t = rand.Next(9);
  82. sb.AppendFormat("{0}", t);
  83. }
  84. return sb.ToString();
  85. }
  86. #endregion
  87. #region 删除最后一个字符之后的字符
  88. /// <summary>
  89. /// 删除最后结尾的一个逗号
  90. /// </summary>
  91. /// <param name="str">字串</param>
  92. /// <returns></returns>
  93. public static string DelLastComma(string str)
  94. {
  95. return str.Substring(0, str.LastIndexOf(","));
  96. }
  97. /// <summary>
  98. /// 删除最后结尾的指定字符后的字符
  99. /// </summary>
  100. /// <param name="str">字串</param>
  101. /// <param name="strchar">指定的字符</param>
  102. /// <returns></returns>
  103. public static string DelLastChar(string str, string strchar)
  104. {
  105. return str.Substring(0, str.LastIndexOf(strchar));
  106. }
  107. /// <summary>
  108. /// 删除最后结尾的长度
  109. /// </summary>
  110. /// <param name="str">字串</param>
  111. /// <param name="Length">删除长度</param>
  112. /// <returns></returns>
  113. public static string DelLastLength(string str, int Length)
  114. {
  115. if (string.IsNullOrEmpty(str))
  116. return "";
  117. str = str.Substring(0, str.Length - Length);
  118. return str;
  119. }
  120. #endregion
  121. }
  122. }