Tools.cs 5.3 KB

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