Tools.cs 4.7 KB

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