实用类

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


namespace Common
{

    public enum LengthInfo
    { less, more, success }

    public static class WTBString
    {
        /// <summary>
        /// 将字符串进行MD5加密处理
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <returns>经MD5加密的字符串</returns>
        public static string GetMD5String(string str)
        {

            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] data =
                System.Text.Encoding.Default.GetBytes(str);
            byte[] md5data = md5.ComputeHash(data);
            md5.Clear();

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < md5data.Length - 1; i++)
            {

                builder.Append(md5data[i].ToString("X2"));
            }
            return builder.ToString();


        }

        /// <summary>
        /// 用户名验证
        /// </summary>
        /// <param name="str">用户名</param>
        /// <returns>bool</returns>
        public static bool GetLoginId(string str)
        {
            return Regex.IsMatch(str, "^\\w+$");
        }

        /// <summary>
        /// 字符长度验证
        /// </summary>
        /// <param name="str">被验证字符</param>
        /// <param name="min">最小长度</param>
        /// <param name="max">最大长度</param>
        /// <returns>返回本命名空间中的枚举 LengthInfo</returns>
        public static LengthInfo CheckLength(string str, int min, int max)
        {
            int len = str.Length;
            if (len < min)
                return LengthInfo.less;
            if (len > max)
                return LengthInfo.more;

            return LengthInfo.success;
        }
        /// <summary>
        /// 判断单个字符是否为中文
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        public static bool DoubleChar(char ch)
        {
            return Regex.IsMatch(ch.ToString(), "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$");
        }
        /// <summary>
        /// 判断字符串是否为中文
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool DobuleChar(string str)
        {
            return Regex.IsMatch(str, "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$");
        }

        /// <summary>
        /// 中英混合时判断字符的长度
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int Length(string str)
        {
            int i = 0;
            foreach (char ch in str)
            {
                if (DoubleChar(ch))
                    i += 2;
                else
                    i++;
            }
            return i;
        }
        /// <summary>
        /// 是否为价格(浮点型不能判断中文数字混合的
        /// </summary>
        /// <param name="price"></param>
        /// <returns></returns>
        public static bool IsPrice(string price)
        {

            return Regex.IsMatch(price, "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$");
        }
        /// <summary>
        /// 是否为正整数
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static bool IsZhengZNum(string a)
        {
            return Regex.IsMatch(a, "^[1-9]\\d*$");
        }
        /// <summary>
        /// 是否为一个数字
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static bool IsNum(string a)
        {
            return Regex.IsMatch(a, "^([+-]?)\\d*\\.?\\d+$");
        }
        /// <summary>
        /// 验证身份证号码
        /// </summary>
        /// <param name="idcard"></param>
        /// <returns></returns>
        public static bool Idcard(string idcard)
        {
            return Regex.IsMatch(idcard, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$");
        }

        /// <summary>
        /// 判断字符串是否是全数字
        /// </summary>
        public static bool IsNums(string str)
        {
            return Regex.IsMatch(str, "^[0-9]+$");
        }
        /// <summary>
        /// 判断字符串是否是邮箱格式
        /// </summary>
        public static bool IsEmail(string str)
        {
            return Regex.IsMatch(str, "^[\\w-]+@[\\w-]+\\.(com|net|cn|org|edu|mil|tv|biz|info)$");
        }

        /// <summary>
        /// 判断字符串是否全是中文
        /// </summary>
        public static bool IsCHStr(string str)
        {
            bool result = true;
            char[] chars = str.ToCharArray();
            foreach (char ch in chars)
            {
                if (!DoubleByte(ch))
                {
                    result = false;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// 判断字符串是否是日期格式
        /// </summary>
        public static bool IsDate(string str)
        {
            try
            {
                Convert.ToDateTime(str);
                return true;
            }
            catch
            {
                return false;
            }
        }


        ///<summary>
        ///是否是电话号码
        ///</summary>
        public static bool IsTel(string str)
        {
            return Regex.IsMatch(str, "^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$");
        }
        ///<summary>
        ///是否是邮编
        ///</summary>
        public static bool IsZipCode(string str)
        {
            return Regex.IsMatch(str, "^\\d{6}$");
        }
        /// <summary>
        /// 手机号
        /// </summary>
        public static bool IsMoblie(string str)
        {
            return Regex.IsMatch(str, "^(13|15|18)[0-9]{9}$");
        }

        /// <summary>
        /// qq号
        /// </summary>
        public static bool IsQQ(string str)
        {
            return Regex.IsMatch(str, "^[1-9]*[1-9][0-9]*$");
        }

        /// <summary>
        /// 判断字符串的长度,一个中文字符长度为2
        /// </summary>
        public static int Len(string str)
        {
            char[] ch = str.ToCharArray();
            int len = 0;
            for (int i = 0; i < ch.Length; i++)
            {
                if (DoubleByte(ch[i]))
                    len += 2;
                else
                    len++;
            }
            return len;
        }

        /// <summary>
        /// 移除字符串两端的空字符,包含全角空格
        /// </summary>
        public static string Trim(string str)
        {
            str = str.Trim();
            char[] ch = new char[1];
            ch[0] = ' ';
            str = str.Trim(ch);
            return str;

        }
        /// <summary>
        /// 根据指定长度截取
        /// </summary>
        public static string Substring(string str, int len)
        {
            char[] chArray = str.ToCharArray();
            int tmplen = len;
            int j = 0;
            for (int i = 0; i < chArray.Length; i++)
            {

                if (j < tmplen)
                {
                    if (DoubleByte(chArray[i]))
                    {
                        len = len - 1;
                        j += 2;
                    }
                    else
                        j++;
                }
                else
                    break;

            }
            int tmpLen = str.Length;
            if (len >= tmpLen)
                return str;
            else
                return str.Substring(0, len) + "…";
        }
        /// <summary>
        /// 获取纯文本
        /// </summary>
        public static string RemoveHtml(string text)
        {

            if (string.IsNullOrEmpty(text))
                return string.Empty;
            text = Regex.Replace(text, "[\\s]{2,}", " ");	//two or more spaces
            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n");	//<br>
            text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " ");	// 
            text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty);	//any other tags
            text = text.Replace("'", "''");
            return text;

        }

        /// <summary>
        /// 获取字符串中第一幅图片地址
        /// </summary>
        public static string GetImgUrl(string str)
        {
            string Pattern = @"<\s*img[^>]*src\s*=\s*(?:'|"")(?<dl>[^>]*?)(?:'|"")[^>]*/\s*>";
            Match m = Regex.Match(str, Pattern);
            return m.Groups["dl"].ToString();
        }

        /// <summary>
        ///按指定模式返回指定长度的随机字符串
        /// </summary>
        /// <param name="len"></param>
        /// <param name="mode">num:仅返回数字;letter:仅返回纯字母;both:由数字及纯字母随机组合的的字符串</param>
        /// <returns></returns>
        public static string GetRandomStr(int len, string mode)
        {
            Random ran = new Random();
            int tem = 0;
            int num;
            string result = "";
            for (int i = 0; i < len; i++)
            {
                num = ran.Next();
                switch (mode)
                {
                    case "num":
                        tem = num % 10 + '0';//生成数字
                        break;
                    case "letter":
                        tem = num % 26 + 'A';//生成字母
                        break;
                    case "both":
                        if (num % 2 == 0)
                            tem = num % 10 + '0';
                        else
                            tem = num % 26 + 'A';
                        break;

                    default:
                        break;
                }

                result += Convert.ToChar(tem).ToString();
            }

            return result;
        }

        /// <summary>
        /// 判断字符是否是双字节字符
        /// </summary>
        private static bool DoubleByte(char ch)
        {
            if (Regex.IsMatch(ch.ToString(), "[^\x00-\xff]|[\u4e00-\u9fa5]"))
                return true;
            return false;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值