using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Common
{
public class Tools
{
///
/// 要分割的字符串 eg: 1,3,10,00
///
///
/// 分割的字符串
///
public static long[] SpitLongArrary(string str, char split = ',')
{
if (string.IsNullOrEmpty(str)) { return Array.Empty(); }
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(); }
string[] strIds = str.Split(split, (char)StringSplitOptions.RemoveEmptyEntries);
int[] infoIdss = Array.ConvertAll(strIds, s => int.Parse(s));
return infoIdss;
}
public static T[] SplitAndConvert(string input, char split = ',')
{
if (string.IsNullOrEmpty(input)) { return Array.Empty(); }
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;
}
///
/// 根据日期获取星期几
///
public static string GetWeekByDate(DateTime dt)
{
var day = new[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
return day[Convert.ToInt32(dt.DayOfWeek.ToString("d"))];
}
///
/// 得到这个月的第几周
///
/// 年月日
/// 传递过来的时间是第几周
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;
}
///
/// 判断一个字符串是否为url
///
///
///
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;
}
}
///
/// 计算密码强度
///
/// 密码字符串
///
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; //由数字、字母、符号构成的密码
}
}
}