文章出處
文章列表
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.Xml; using System.Xml.Serialization; namespace Lbb.Cx.Ciac.Utility.Code { /// <summary> /// 字符串擴展類 /// </summary> public static class StringExtension { /// <summary> /// 用于判斷是否為空字符 /// </summary> /// <param name="s"></param> /// <returns></returns> public static bool IsBlank(this string s) { return s == null || (s.Trim().Length == 0); } /// <summary> /// 用于判斷是否不為空字符 /// </summary> /// <param name="s"></param> /// <returns></returns> public static bool IsNotBlank(this string s) { return !s.IsBlank(); } /// <summary> /// 判斷是否為有效的Email地址 /// </summary> /// <param name="s"></param> /// <returns></returns> public static bool IsValidEmail(this string s) { if (!s.IsBlank()) { //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; } /// <summary> /// 驗證是否是合法的電話號碼 /// </summary> /// <param name="s"></param> /// <returns></returns> public static bool IsValidPhone(this string s) { if (!s.IsBlank()) { return Regex.IsMatch(s, @"^\+?((\d{2,4}(-)?)|(\(\d{2,4}\)))*(\d{0,16})*$"); } return true; } /// <summary> /// 驗證是否是合法的手機號碼 /// </summary> /// <param name="s"></param> /// <returns></returns> public static bool IsValidMobile(this string s) { if (!s.IsBlank()) { return Regex.IsMatch(s, @"^\+?\d{0,4}?[1][3-8]\d{9}$"); } return false; } /// <summary> /// 驗證是否是合法的郵編 /// </summary> /// <param name="s"></param> /// <returns></returns> public static bool IsValidZipCode(this string s) { if (!s.IsBlank()) { return Regex.IsMatch(s, @"[1-9]\d{5}(?!\d)"); } return true; } /// <summary> /// 驗證是否是合法的傳真 /// </summary> /// <param name="s"></param> /// <returns></returns> public static bool IsValidFax(this string s) { if (!s.IsBlank()) { 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; } /// <summary> /// 檢查字符串是否為有效的INT32數字 /// </summary> /// <param name="val"></param> /// <returns></returns> public static bool IsInt32(this string val) { if (IsBlank(val)) return false; Int32 k; return Int32.TryParse(val, out k); } /// <summary> /// 檢查字符串是否為有效的INT64數字 /// </summary> /// <param name="val"></param> /// <returns></returns> public static bool IsInt64(this string val) { if (IsBlank(val)) return false; Int64 k; return Int64.TryParse(val, out k); } /// <summary> /// 檢查字符串是否為有效的Decimal /// </summary> /// <param name="val"></param> /// <returns></returns> public static bool IsDecimal(this string val) { if (IsBlank(val)) return false; decimal d; return Decimal.TryParse(val, out d); } /// <summary> /// 將字符串轉換成MD5加密字符串 /// </summary> /// <param name="orgStr"></param> /// <returns></returns> public static string ToMD5(this string orgStr) { MD5 md5Hasher = MD5.Create(); // Convert the input string to a byte array and compute the hash. byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(orgStr)); // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // Return the hexadecimal string. return sBuilder.ToString(); } /// <summary> /// 將對象序列化成XML /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static string ToXML<T>(this T obj) where T : class { return ToXML(obj, Encoding.Default.BodyName); } public static string ToXML<T>(this T obj, string encodeName) where T : class { if (obj == null) throw new ArgumentNullException("obj", "obj is null."); if (obj is string) throw new ApplicationException("obj can't be string object."); Encoding en = Encoding.GetEncoding(encodeName); XmlSerializer serial = new XmlSerializer(typeof(T)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); MemoryStream ms = new MemoryStream(); XmlTextWriter xt = new XmlTextWriter(ms, en); serial.Serialize(xt, obj, ns); xt.Close(); ms.Close(); return en.GetString(ms.ToArray()); } /// <summary> /// 將XML字符串反序列化成對象實體 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="s"></param> /// <returns></returns> public static T Deserial<T>(this string s) where T : class { return Deserial<T>(s, Encoding.Default.BodyName); } public static T Deserial<T>(this string s, string encodeName) where T : class { if (s.IsBlank()) { throw new ApplicationException("xml string is null or empty."); } XmlSerializer serial = new XmlSerializer(typeof(T)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); return (T)serial.Deserialize(new StringReader(s)); } /// <summary> /// 獲取擴展名 /// </summary> /// <param name="s"></param> /// <returns></returns> public static string GetExt(this string s) { string ret = string.Empty; if (s.Contains('.')) { string[] temp = s.Split('.'); ret = temp[temp.Length - 1]; } return ret; } /// <summary> /// 驗證QQ格式 /// </summary> /// <param name="s"></param> /// <returns></returns> public static bool IsValidQQ(this string s) { if (!s.IsBlank()) { return Regex.IsMatch(s, @"^[1-9]\d{4,15}$"); } return false; } /// <summary> /// 字符串轉成Int /// </summary> /// <param name="s"></param> /// <returns></returns> public static int ToInt(this string s) { int res; int.TryParse(s, out res); return res; } } }
文章列表
全站熱搜