Tools.cs 5.2 KB

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