using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Newtonsoft.Json; namespace Myshipping.Core { /// /// string 拓展 /// public static class StringExtension { /// /// 隐藏中间字符串 /// /// /// public static string GetHideCentre(this string str) { if (str.Length > 8) { return string.Format("{0}***{1}", str.Substring(0, 4), str.Substring(str.Length - 4, 4)); } else if (str.Length > 5) { return string.Format("{0}***{1}", str.Substring(0, 2), str.Substring(str.Length - 2, 2)); } else if (str.Length > 4) { return string.Format("{0}***{1}", str.Substring(0, 1), str.Substring(str.Length - 1, 1)); } else { return GetHideHead(str); } } /// /// 隐藏头部 /// /// /// public static string GetHideHead(this string str) { var length = 1; length = length > str.Length ? str.Length : length; if (str.Length < 4) { length = 1; if (str.Length == 1) { return str; } return "**" + str.Substring(str.Length - length, length); } length = 4; return "****" + str.Substring(str.Length - length, length); } /// /// 用于判断是否为空字符 /// /// /// public static bool IsNull(this string s) { return s == null || (s.Trim().Length == 0); } /// /// 用于判断是否为非空字符 /// /// /// public static bool IsNotNull(this string s) { return !s.IsNull(); } #region 加密算法 /// /// DES对称加密字符串 /// /// 待加密的字符串 /// 加密密钥,要求为16位 /// 加密成功返回加密后的字符串,失败返回源串 public static string DESEncrypt(this string encryptString, string key) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(key); //用于对称算法的初始化向量(默认值)。 byte[] rgbIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); Aes dCSP = Aes.Create(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch (Exception ex) { throw ex; } } /// /// DES对称解密字符串 /// /// 待解密的字符串 /// 解密密钥,要求16位 /// public static string DESDecrypt(this string decryptString, string key) { try { //用于对称算法的初始化向量(默认值) byte[] Keys = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; byte[] rgbKey = Encoding.UTF8.GetBytes(key); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); Aes DCSP = Aes.Create(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } } /// /// 将字符串转换成MD5加密字符串 /// /// /// public static string ToMd5(this string orgStr) { using (var md5 = MD5.Create()) { var encoding = Encoding.UTF8; var encryptedBytes = md5.ComputeHash(encoding.GetBytes(orgStr)); var sb = new StringBuilder(32); foreach (var bt in encryptedBytes) { sb.Append(bt.ToString("x").PadLeft(2, '0')); } return sb.ToString().ToLower(); } } #endregion /// /// 获取扩展名 /// /// /// public static string GetExt(this string s) { var ret = string.Empty; if (!s.Contains('.')) return ret; var temp = s.Split('.'); ret = temp[temp.Length - 1]; return ret; } /// /// 验证QQ格式 /// /// /// public static bool IsQq(this string s) { return s.IsNull() || Regex.IsMatch(s, @"^[1-9]\d{4,15}$"); } /// /// 将字符串根据分隔符转换我List /// /// /// 分隔符 /// public static List ToListString(this string str, char split) { return new List(str.Split(split)); } /// /// 判断是否为有效的Email地址 /// /// /// public static bool IsEmail(this string s) { if (!s.IsNull()) { //return Regex.IsMatch(s, // @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" + // @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"); const string pattern = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"; return Regex.IsMatch(s, pattern); } return false; } /// /// 判断是否是url /// /// /// public static bool IsUrl(this string s) { if (!s.IsNull()) { const string pattern = @"^(http://|https://)?((?:[A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\.)+([A-Za-z]+)[/\?\:]?.*$"; return Regex.IsMatch(s, pattern); } return false; } /// /// 验证是否是合法的电话号码 /// /// /// public static bool IsPhone(this string s) { if (!s.IsNull()) { return Regex.IsMatch(s, @"^\+?((\d{2,4}(-)?)|(\(\d{2,4}\)))*(\d{0,16})*$"); } return true; } /// /// 验证是否是合法的手机号码 /// /// /// public static bool IsMobile(this string s) { if (!s.IsNull()) { return Regex.IsMatch(s, @"^\+?\d{0,4}?[1][3-8]\d{9}$"); } return false; } /// /// 验证是否是合法的邮编 /// /// /// public static bool IsZipCode(this string s) { if (!s.IsNull()) { return Regex.IsMatch(s, @"[1-9]\d{5}(?!\d)"); } return true; } /// /// 验证是否是合法的传真 /// /// /// public static bool IsFax(this string s) { if (!s.IsNull()) { return Regex.IsMatch(s, @"(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)"); } return true; } /// /// 检查字符串是否为有效的int数字 /// /// /// public static bool Isint(this string val) { if (IsNull(val)) return false; int k; return int.TryParse(val, out k); } /// /// 字符串转数字,未转换成功返回0 /// /// /// public static int ToInt(this string val) { if (IsNull(val)) return 0; int k; return int.TryParse(val, out k) ? k : 0; } /// /// 将int间隔字符串抓换为list /// /// /// /// public static List ToIntList(this string str, char Split = ',') { return str.Split(Split).strToIntArray().ToList(); } /// /// 判断是否是json字符串 /// /// /// public static bool IsJson(this string str) { try { if (str.IsNull()) return false; JsonConvert.DeserializeObject(str); return true; } catch { return false; } } /// /// 检查字符串是否为有效的INT64数字 /// /// /// public static bool IsInt64(this string val) { if (IsNull(val)) return false; long k; return long.TryParse(val, out k); } /// /// 验证是否是身份证 /// /// /// public static bool IsCardId(this string ID) { var r = false; if (IsNull(ID)) r = false; if (ID.Length == 15) { var date = $"19{ID.Substring(6, 2)}-{ID.Substring(8, 2)}-{ID.Substring(10, 2)}"; DateTime dt; return DateTime.TryParse(date, out dt); } else if (ID.Length == 18) { var date = $"{ID.Substring(6, 4)}-{ID.Substring(10, 2)}-{ID.Substring(12, 2)}"; DateTime dt; return DateTime.TryParse(date, out dt); } else { r = false; } return r; } /// /// 检查字符串是否为有效的double /// /// /// public static bool IsDecimal(this string val) { if (IsNull(val)) return false; double d; return double.TryParse(val, out d); } /// ///检测字符串是否是时间类型 /// /// /// public static bool IsDateTime(this string val) { if (IsNull(val)) return false; DateTime d; return DateTime.TryParse(val, out d); } /// /// 字符串转时间 非时间返回空 /// /// /// public static DateTime? ToDateTime(this string val) { if (IsDateTime(val)) { return DateTime.Parse(val); } else { return null; } } /// /// 将时间类型字符转时间戳 /// /// /// public static long ToTimestamp(this string Val) { var time = Val.ToDateTime(); return time != null ? ((DateTime)time).ToTimeStamp() : 0; } /// /// 从左边截取N个字符 /// /// /// public static string GetLeftStr(this string val, int count = 0) { if (count > val.Length) return null; return val.Substring(0, count); } /// ///从右边截取N个字符 /// /// /// /// public static string GetRightStr(this string val, int count = 0) { if (count > val.Length) return null; return val.Substring(val.Length - count, count); } } }