123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- using Infrastructure;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace ZR.Common
- {
- public class Tools
- {
- /// <summary>
- /// 要分割的字符串 eg: 1,3,10,00
- /// </summary>
- /// <param name="str"></param>
- /// <param name="split">分割的字符串</param>
- /// <returns></returns>
- public static long[] SpitLongArrary(string str, char split = ',')
- {
- if (string.IsNullOrEmpty(str)) { return Array.Empty<long>(); }
- str = str.TrimStart(split).TrimEnd(split);
- string[] strIds = str.Split(split, (char)StringSplitOptions.RemoveEmptyEntries);
- long[] infoIdss = Array.ConvertAll(strIds, s => long.Parse(s));
- return infoIdss;
- }
- public static int[] SpitIntArrary(string str, char split = ',')
- {
- if (string.IsNullOrEmpty(str)) { return Array.Empty<int>(); }
- string[] strIds = str.Split(split, (char)StringSplitOptions.RemoveEmptyEntries);
- int[] infoIdss = Array.ConvertAll(strIds, s => int.Parse(s));
- return infoIdss;
- }
- public static T[] SplitAndConvert<T>(string input, char split = ',')
- {
- if (string.IsNullOrEmpty(input)) { return Array.Empty<T>(); }
- string[] parts = input.Split(split, (char)StringSplitOptions.RemoveEmptyEntries);
- T[] result = Array.ConvertAll(parts, s => (T)Convert.ChangeType(s, typeof(T)));
- //for (int i = 0; i < parts.Length; i++)
- //{
- // result[i] = (T)Convert.ChangeType(parts[i], typeof(T));
- //}
- return result;
- }
- /// <summary>
- /// 根据日期获取星期几
- /// </summary>
- public static string GetWeekByDate(DateTime dt)
- {
- var day = new[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
- return day[Convert.ToInt32(dt.DayOfWeek.ToString("d"))];
- }
- /// <summary>
- /// 得到这个月的第几周
- /// </summary>
- /// <param name="daytime">年月日</param>
- /// <returns>传递过来的时间是第几周</returns>
- public static int GetWeekNumInMonth(DateTime daytime)
- {
- int dayInMonth = daytime.Day;
- //本月第一天
- DateTime firstDay = daytime.AddDays(1 - daytime.Day);
- //本月第一天是周几
- int weekday = (int)firstDay.DayOfWeek == 0 ? 7 : (int)firstDay.DayOfWeek;
- //本月第一周有几天
- int firstWeekEndDay = 7 - (weekday - 1);
- //当前日期和第一周之差
- int diffday = dayInMonth - firstWeekEndDay;
- diffday = diffday > 0 ? diffday : 1;
- //当前是第几周,如果整除7就减一天
- int weekNumInMonth = ((diffday % 7) == 0
- ? (diffday / 7 - 1)
- : (diffday / 7)) + 1 + (dayInMonth > firstWeekEndDay ? 1 : 0);
- return weekNumInMonth;
- }
- /// <summary>
- /// 判断一个字符串是否为url
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsUrl(string str)
- {
- try
- {
- string Url = @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$";
- return Regex.IsMatch(str, Url);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- return false;
- }
- }
- public static bool CheckUserName(string str)
- {
- try
- {
- string rg = @"^[a-z][a-z0-9-_]*$";
- return Regex.IsMatch(str, rg);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- return false;
- }
- }
- /// <summary>
- /// 计算密码强度
- /// </summary>
- /// <param name="password">密码字符串</param>
- /// <returns></returns>
- public static bool PasswordStrength(string password)
- {
- //空字符串强度值为0
- if (string.IsNullOrEmpty(password)) return false;
- //字符统计
- int iNum = 0, iLtt = 0, iSym = 0;
- foreach (char c in password)
- {
- if (c >= '0' && c <= '9') iNum++;
- else if (c >= 'a' && c <= 'z') iLtt++;
- else if (c >= 'A' && c <= 'Z') iLtt++;
- else iSym++;
- }
- if (iLtt == 0 && iSym == 0) return false; //纯数字密码
- if (iNum == 0 && iLtt == 0) return false; //纯符号密码
- if (iNum == 0 && iSym == 0) return false; //纯字母密码
- if (password.Length >= 6 && password.Length < 16) return true;//长度不大于6的密码
- if (iLtt == 0) return true; //数字和符号构成的密码
- if (iSym == 0) return true; //数字和字母构成的密码
- if (iNum == 0) return true; //字母和符号构成的密码
- return true; //由数字、字母、符号构成的密码
- }
- ///<summary>
- ///生成随机字符串
- ///</summary>
- ///<param name="length">目标字符串的长度</param>
- ///<param name="useNum">是否包含数字,1=包含,默认为包含</param>
- ///<param name="useLow">是否包含小写字母,1=包含,默认为包含</param>
- ///<param name="useUpp">是否包含大写字母,1=包含,默认为包含</param>
- ///<param name="useSpe">是否包含特殊字符,1=包含,默认为不包含</param>
- ///<param name="custom">要包含的自定义字符,直接输入要包含的字符列表</param>
- ///<returns>指定长度的随机字符串</returns>
- public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
- {
- byte[] b = new byte[4];
- System.Security.Cryptography.RandomNumberGenerator.Create().GetBytes(b);
- Random r = new(BitConverter.ToInt32(b, 0));
- string s = null, str = custom;
- if (useNum == true) { str += "0123456789"; }
- if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
- if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
- if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
- for (int i = 0; i < length; i++)
- {
- s += str.Substring(r.Next(0, str.Length - 1), 1);
- }
- return s;
- }
- }
- }
|