using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace SCC.Common { /// /// 系统扩展方法 /// public static class SystemExtensions { #region 字符串类扩展方法 /// /// 生成手机号码的友好显示 /// 形如133****1234 /// /// 电话号码 /// public static string ToMobileNumReplaceShow(this string s) { if (string.IsNullOrWhiteSpace(s)) return string.Empty; s = s.Trim(); Regex reg = new Regex(@"^1[34578]\d{9}$"); if (reg.IsMatch(s)) s = s.Remove(3, 4).Insert(3, "****"); return s; } /// /// 该字符串是否为电话号码 /// /// 字符串 /// public static bool IsMobileString(this string s) { if (string.IsNullOrWhiteSpace(s)) return false; s = s.Trim(); Regex reg = new Regex(@"^1[34578]\d{9}$"); return reg.IsMatch(s); } /// /// 将string[]类型转换成string(用|分隔) /// /// /// public static string ArrayToString(this string[] str) { if (str == null) return ""; StringBuilder sb = new StringBuilder(str.Length * 50); foreach (var item in str) { sb.Append(item + "|"); } return sb.ToString().TrimEnd('|'); } #endregion #region 时间类扩展方法 /// /// 生成时间的友好提示 /// 如:刚刚,多少秒前,多少小时前,多少天前 /// /// 生成友好提示的时间对象 /// 日期格式(可空,默认yyyy-MM-dd) /// 是否显示时间(可控,默认显示,格式HH:mm) /// public static string ToFriendlyString(this DateTime dateTime, string dateFormat = "", bool showTime = true) { if (dateTime == DateTime.MinValue) return "-"; if (string.IsNullOrEmpty(dateFormat)) dateFormat = "yyyy-MM-dd"; string timeFormat = "HH:mm"; DateTime userDate = dateTime; DateTime userNow = DateTime.Now; if (userDate > userNow) { return userDate.ToString(dateFormat + (showTime ? " " + timeFormat : "")); } TimeSpan intervalTime = userNow - userDate; int intervalDays; if (userNow.Year == userDate.Year) intervalDays = userNow.DayOfYear - userDate.DayOfYear; else intervalDays = intervalTime.Days + 1; string result = "{0}"; if (showTime) result = "{0}" + " " + userDate.ToString(timeFormat); if (intervalDays > 7) { if (userDate.Year == userNow.Year) { return string.Format("{0}月{1}日{2}", userDate.Month, userDate.Day, showTime ? " " + userDate.ToString(timeFormat) : ""); } return userDate.ToString(dateFormat + (showTime ? " " + timeFormat : "")); } if (intervalDays >= 3) { string timeScope = string.Format("{0}天之前", intervalDays); return string.Format(result, timeScope); } if (intervalDays == 2) { return string.Format(result, "前天"); } if (intervalDays == 1) { return string.Format(result, "昨天"); } if (intervalTime.Hours >= 1) return string.Format("{0}小时之前", intervalTime.Hours); if (intervalTime.Minutes >= 1) return string.Format("{0}分钟之前", intervalTime.Minutes); if (intervalTime.Seconds >= 1) return string.Format("{0}秒之前", intervalTime.Seconds); return "现在"; } #endregion #region 字典类扩展方法 /// /// 从字典中获取key的值,若不存在或返回指定类型失败,则返回指定类型的默认值 /// /// 指定返回类型 /// 字典 /// 获取的key /// 指定返回类型的默认值 /// public static T Get(this IDictionary dictionary, string key, T defaultValue) { if (dictionary.ContainsKey(key)) { object obj2; dictionary.TryGetValue(key, out obj2); return ChangeType(obj2, defaultValue); } return defaultValue; } /// /// 向字典中尝试添加键值 /// /// 该字典键类型 /// 该字典值类型 /// 字典 /// 键 /// 值 /// public static IDictionary TryAdd(this IDictionary dictionary, TKey key, TValue value) { if (value != null && !dictionary.ContainsKey(key)) { dictionary[key] = value; } return dictionary; } #endregion #region 强制类型转换 public static T ChangeType(object value) { return ChangeType(value, default(T)); } public static T ChangeType(object value, T defalutValue) { if (value != null) { Type nullableType = typeof(T); if (!nullableType.IsInterface && (!nullableType.IsClass || (nullableType == typeof(string)))) { if (nullableType.IsGenericType && (nullableType.GetGenericTypeDefinition() == typeof(Nullable<>))) { return (T)Convert.ChangeType(value, Nullable.GetUnderlyingType(nullableType)); } if (nullableType.IsEnum) { return (T)Enum.Parse(nullableType, value.ToString()); } return (T)Convert.ChangeType(value, nullableType); } if (value is T) { return (T)value; } } return defalutValue; } #endregion #region 本系统时间扩展 /// /// 生成指定的时间字符串 /// yyyy-MM-dd HH:mm:ss /// /// /// public static string ToSCCDateTimeString1(this DateTime datetime) { return datetime.ToString("yyyy-MM-dd HH:mm:ss"); } /// /// 生成指定的时间字符串 /// yyyyMMddHHmmss /// /// /// public static string ToSCCDateTimeString2(this DateTime datetime) { return datetime.ToString("yyyyMMddHHmmss"); } /// /// 生成指定的时间字符串 /// yyyy-MM-dd /// /// /// public static string ToSCCDateTimeString3(this DateTime datetime) { return datetime.ToString("yyyy-MM-dd"); } #endregion } }