csharp/8720826/emud/src/Emprise.Domain.Core/Extensions/StringExtension.cs

StringExtension.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace Emprise.Domain.Core.Extensions
{
    public static clast StringExtension
    {
        public static bool Eq(this string input, string toCompare, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
        {
            if (input == null)
            {
                return toCompare == null;
            }
            return input.Equals(toCompare, comparison);
        }
        
        public static Guid? ToGuid(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return null;
            }
            Guid id;
            if (Guid.TryParse(str, out id))
            {
                return id;
            }
            return null;
        }

        public static int? ToInt32(this string str)
        {
            int value;
            if (int.TryParse(str, out value))
            {
                return value;
            }
            return null;
        }
        
        public static bool IsEmail(this string value)
        {
            var reg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            return string.IsNullOrEmpty(value) == false && reg.IsMatch(value);
        }

        public static string ToMd5(this string str)
        {
            byte[] b = Encoding.UTF8.GetBytes(str);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
            {
                ret = ret + b[i].ToString("x").PadLeft(2, '0');
            }
            return ret;
        }


        ///  
        /// DES加密 
        ///  
        /// 待加密的字符串 
        /// 加密密钥 
        /// 加密后的字符串 
        public static string ToDesEncrypt(this string text, string sKey= "[email protected]")
        {
            byte[] inputArray = Encoding.UTF8.GetBytes(text);
            var tripleDES = TripleDES.Create();
            var byteKey = Encoding.UTF8.GetBytes(sKey);
            tripleDES.Key = byteKey;
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tripleDES.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }


        ///  
        /// DES解密 
        ///  
        /// 待解密的字符串 
        /// 解密密钥 
        /// 解密后的字符串 
        public static string ToDesDecrypt(this string text, string sKey = "[email protected]")
        {
            try
            {
                byte[] inputArray = Convert.FromBase64String(text);
                var tripleDES = TripleDES.Create();
                var byteKey = Encoding.UTF8.GetBytes(sKey);
                tripleDES.Key = byteKey;
                tripleDES.Mode = CipherMode.ECB;
                tripleDES.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = tripleDES.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
                return Encoding.UTF8.GetString(resultArray);

            }
            catch
            {
                return "";
            }
        }


        public static string JoinString(this IEnumerable values)
        {
            return JoinString(values, ",");
        }

        public static string JoinString(this IEnumerable values, string split)
        {
            var result = values.Aggregate(string.Empty, (current, value) => current + (split + value));
            result = result.TrimStart(split.ToCharArray());
            return result;
        }



        /// 
        /// 字符串分隔转换为List
        /// 
        /// 
        /// 
        /// 
        /// 
        public static List StrToList(this string str, char separator = ',')
        {
            var list = new List();
            if (string.IsNullOrEmpty(str))
            {
                return list;
            }
            foreach (var c in str.Split(separator))
            {
                if (c.Length == 0) { continue; }

                try
                {
                    T result = (T)Convert.ChangeType(c, typeof(T));
                    if (result == null) { continue; }
                    list.Add(result);
                }
                catch (Exception)
                {

                }

            }
            return list;
        }

        public static bool IsChinese(this string text)
        {
            Regex rx = new Regex("^[\u4e00-\u9fbb]{0,}$");
            return rx.IsMatch(text);
        }

        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        
        private static char[] constant =
        {
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
        };

        public static string GenerateRandom(this String obj, int Length)
        {
            StringBuilder newRandom = new StringBuilder(constant.Length);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(constant.Length)]);
            }
            return newRandom.ToString();
        }
    }
}