Tools.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using Infrastructure;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace ZR.Common
  7. {
  8. public class Tools
  9. {
  10. /// <summary>
  11. /// 要分割的字符串 eg: 1,3,10,00
  12. /// </summary>
  13. /// <param name="str"></param>
  14. /// <param name="split">分割的字符串</param>
  15. /// <returns></returns>
  16. public static long[] SpitLongArrary(string str, char split = ',')
  17. {
  18. if (string.IsNullOrEmpty(str)) { return Array.Empty<long>(); }
  19. str = str.TrimStart(split).TrimEnd(split);
  20. string[] strIds = str.Split(split, (char)StringSplitOptions.RemoveEmptyEntries);
  21. long[] infoIdss = Array.ConvertAll(strIds, s => long.Parse(s));
  22. return infoIdss;
  23. }
  24. public static int[] SpitIntArrary(string str, char split = ',')
  25. {
  26. if (string.IsNullOrEmpty(str)) { return Array.Empty<int>(); }
  27. string[] strIds = str.Split(split, (char)StringSplitOptions.RemoveEmptyEntries);
  28. int[] infoIdss = Array.ConvertAll(strIds, s => int.Parse(s));
  29. return infoIdss;
  30. }
  31. public static T[] SplitAndConvert<T>(string input, char split = ',')
  32. {
  33. if (string.IsNullOrEmpty(input)) { return Array.Empty<T>(); }
  34. string[] parts = input.Split(split, (char)StringSplitOptions.RemoveEmptyEntries);
  35. T[] result = Array.ConvertAll(parts, s => (T)Convert.ChangeType(s, typeof(T)));
  36. //for (int i = 0; i < parts.Length; i++)
  37. //{
  38. // result[i] = (T)Convert.ChangeType(parts[i], typeof(T));
  39. //}
  40. return result;
  41. }
  42. /// <summary>
  43. /// 根据日期获取星期几
  44. /// </summary>
  45. public static string GetWeekByDate(DateTime dt)
  46. {
  47. var day = new[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
  48. return day[Convert.ToInt32(dt.DayOfWeek.ToString("d"))];
  49. }
  50. /// <summary>
  51. /// 得到这个月的第几周
  52. /// </summary>
  53. /// <param name="daytime">年月日</param>
  54. /// <returns>传递过来的时间是第几周</returns>
  55. public static int GetWeekNumInMonth(DateTime daytime)
  56. {
  57. int dayInMonth = daytime.Day;
  58. //本月第一天
  59. DateTime firstDay = daytime.AddDays(1 - daytime.Day);
  60. //本月第一天是周几
  61. int weekday = (int)firstDay.DayOfWeek == 0 ? 7 : (int)firstDay.DayOfWeek;
  62. //本月第一周有几天
  63. int firstWeekEndDay = 7 - (weekday - 1);
  64. //当前日期和第一周之差
  65. int diffday = dayInMonth - firstWeekEndDay;
  66. diffday = diffday > 0 ? diffday : 1;
  67. //当前是第几周,如果整除7就减一天
  68. int weekNumInMonth = ((diffday % 7) == 0
  69. ? (diffday / 7 - 1)
  70. : (diffday / 7)) + 1 + (dayInMonth > firstWeekEndDay ? 1 : 0);
  71. return weekNumInMonth;
  72. }
  73. /// <summary>
  74. /// 判断一个字符串是否为url
  75. /// </summary>
  76. /// <param name="str"></param>
  77. /// <returns></returns>
  78. public static bool IsUrl(string str)
  79. {
  80. try
  81. {
  82. string Url = @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$";
  83. return Regex.IsMatch(str, Url);
  84. }
  85. catch (Exception ex)
  86. {
  87. Console.WriteLine(ex.Message);
  88. return false;
  89. }
  90. }
  91. public static bool CheckUserName(string str)
  92. {
  93. try
  94. {
  95. string rg = @"^[a-z][a-z0-9-_]*$";
  96. return Regex.IsMatch(str, rg);
  97. }
  98. catch (Exception ex)
  99. {
  100. Console.WriteLine(ex.Message);
  101. return false;
  102. }
  103. }
  104. /// <summary>
  105. /// 计算密码强度
  106. /// </summary>
  107. /// <param name="password">密码字符串</param>
  108. /// <returns></returns>
  109. public static bool PasswordStrength(string password)
  110. {
  111. //空字符串强度值为0
  112. if (string.IsNullOrEmpty(password)) return false;
  113. //字符统计
  114. int iNum = 0, iLtt = 0, iSym = 0;
  115. foreach (char c in password)
  116. {
  117. if (c >= '0' && c <= '9') iNum++;
  118. else if (c >= 'a' && c <= 'z') iLtt++;
  119. else if (c >= 'A' && c <= 'Z') iLtt++;
  120. else iSym++;
  121. }
  122. if (iLtt == 0 && iSym == 0) return false; //纯数字密码
  123. if (iNum == 0 && iLtt == 0) return false; //纯符号密码
  124. if (iNum == 0 && iSym == 0) return false; //纯字母密码
  125. if (password.Length >= 6 && password.Length < 16) return true;//长度不大于6的密码
  126. if (iLtt == 0) return true; //数字和符号构成的密码
  127. if (iSym == 0) return true; //数字和字母构成的密码
  128. if (iNum == 0) return true; //字母和符号构成的密码
  129. return true; //由数字、字母、符号构成的密码
  130. }
  131. ///<summary>
  132. ///生成随机字符串
  133. ///</summary>
  134. ///<param name="length">目标字符串的长度</param>
  135. ///<param name="useNum">是否包含数字,1=包含,默认为包含</param>
  136. ///<param name="useLow">是否包含小写字母,1=包含,默认为包含</param>
  137. ///<param name="useUpp">是否包含大写字母,1=包含,默认为包含</param>
  138. ///<param name="useSpe">是否包含特殊字符,1=包含,默认为不包含</param>
  139. ///<param name="custom">要包含的自定义字符,直接输入要包含的字符列表</param>
  140. ///<returns>指定长度的随机字符串</returns>
  141. public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
  142. {
  143. byte[] b = new byte[4];
  144. System.Security.Cryptography.RandomNumberGenerator.Create().GetBytes(b);
  145. Random r = new(BitConverter.ToInt32(b, 0));
  146. string s = null, str = custom;
  147. if (useNum == true) { str += "0123456789"; }
  148. if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
  149. if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
  150. if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
  151. for (int i = 0; i < length; i++)
  152. {
  153. s += str.Substring(r.Next(0, str.Length - 1), 1);
  154. }
  155. return s;
  156. }
  157. }
  158. }