爱思开
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.

130 lines
3.7 KiB

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