using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace ZR.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 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; } } } }