码迷,mamicode.com
首页 > 其他好文 > 详细

常用的扩展方法.

时间:2015-04-28 11:11:52      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

注:以下方法皆为收集而来

 

/// <summary>
    /// 自定义扩展类
    /// </summary>
    public static class IExtension
    {
        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        /// <summary>
        /// 验证字符串是否由正负号(+-)、数字、小数点构成,并且最多只有一个小数点
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumeric(this string str)
        {
            Regex regex = new Regex(@"^[+-]?\d+[.]?\d*$");
            return regex.IsMatch(str);
        }

        /// <summary>
        /// 验证字符串是否仅由[0-9]构成
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumericOnly(this string str)
        {
            Regex regex = new Regex("[0-9]");
            return regex.IsMatch(str);
        }

        /// <summary>
        /// 验证字符串是否由字母和数字构成
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumericOrLetters(this string str)
        {
            Regex regex = new Regex("[a-zA-Z0-9]");
            return regex.IsMatch(str);
        }

        /// <summary>
        /// 验证字符串是否符合正则
        /// </summary>
        /// <param name="s">字符串</param>
        /// <param name="pattern">正则</param>
        /// <returns>bool</returns>
        public static bool IsMatch(this string s, string pattern)
        {
            if (s == null) return false;
            else return Regex.IsMatch(s, pattern);
        }

        /// <summary>
        /// 验证字符串是否符合正则
        /// </summary>
        /// <param name="s">字符串</param>
        /// <param name="pattern">正则</param>
        /// <returns>string</returns>
        public static string Match(this string s, string pattern)
        {
            if (s == null) return "";
            return Regex.Match(s, pattern).Value;
            //--------------example--------------
            //bool b = "12345".IsMatch(@"\d+");
            //string s = "ldp615".Match("[a-zA-Z]+");
        }


        /// <summary>
        /// 获取字符串长度。与string.Length不同的是,该方法将中文作 2 个字符计算。
        /// </summary>
        /// <param name="str">目标字符串</param>
        /// <returns></returns>
        public static int GetLength(this string str)
        {
            if (str == null || str.Length == 0) { return 0; }
            int l = str.Length;
            int realLen = l;

            #region 计算长度
            int clen = 0;//当前长度 
            while (clen < l)
            {
                //每遇到一个中文,则将实际长度加一。
                if ((int)str[clen] > 128) { realLen++; }
                clen++;
            }
            #endregion
            return realLen;
        }

        /// <summary>
        /// 将形如 10.1MB 格式对用户友好的文件大小字符串还原成真实的文件大小,单位为字节。
        /// </summary>
        /// <param name="formatedSize">形如 10.1MB 格式的文件大小字符串</param>
        /// <remarks>
        /// 参见:<see cref="uoLib.Common.Functions.FormatFileSize(long)"/>
        /// </remarks>
        /// <returns></returns>
        public static long GetFileSizeFromString(this string formatedSize)
        {
            if (IsNullOrEmptyStr(formatedSize)) throw new ArgumentNullException("formatedSize");
            long size;
            if (long.TryParse(formatedSize, out size)) return size;
            //去掉数字分隔符
            formatedSize = formatedSize.Replace(",", "");
            Regex re = new Regex(@"^([\d\.]+)((?:TB|GB|MB|KB|Bytes))$");
            if (re.IsMatch(formatedSize))
            {
                MatchCollection mc = re.Matches(formatedSize);
                Match m = mc[0];
                double s = double.Parse(m.Groups[1].Value);
                switch (m.Groups[2].Value)
                {
                    case "TB":
                        s *= 1099511627776;
                        break;
                    case "GB":
                        s *= 1073741824;
                        break;
                    case "MB":
                        s *= 1048576;
                        break;
                    case "KB":
                        s *= 1024;
                        break;
                }
                size = (long)s;
                return size;
            }
            throw new ArgumentException("formatedSize");
        }

        /// <summary>
        /// 根据文件夹命名规则验证字符串是否符合文件夹格式
        /// </summary>
        //public static bool IsFolderName(this string folderName)
        //{
        //    if (IsNullOrEmptyStr(folderName)) { return false; }
        //    else
        //    {
        //        // 不能以 “.” 开头
        //        folderName = folderName.Trim().ToLower();
        //        // “nul”、“aux”、“con”、“com1”、“lpt1”不能为文件夹/文件的名称
        //        // 作为文件夹,只需满足名称不为这几个就行。
        //        switch (folderName)
        //        {
        //            case "nul":
        //            case "aux":
        //            case "con":
        //            case "com1":
        //            case "lpt1":
        //                return false;
        //            default:
        //                break;

        //        }
        //        Regex re = new Regex(RegexPatterns.FolderName, RegexOptions.IgnoreCase);
        //        return re.IsMatch(folderName);
        //    }
        //}

        /// <summary>
        /// 验证是否是整形
        /// </summary>
        /// <param name="s"></param>
        /// <returns>bool</returns>
        public static bool IsInt(this string s)
        {
            int i;
            return int.TryParse(s, out i);
        }

        /// <summary>
        /// 验证是否是整形
        /// </summary>
        /// <param name="s"></param>
        /// <returns>int</returns>
        public static int ToInt(this string s)
        {
            return int.Parse(s);
        }

        /// <summary>
        /// 转换时间及小数
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToExtensionString(this Object obj)
        {
            string s = string.Empty;
            if (obj == null) return s;
            if (obj is DateTime)
                s = Convert.ToDateTime(obj).ToString("yyyy-MM-dd");
            else if (obj is Decimal)
                s = Convert.ToDecimal(obj).ToString("###,##0.00");
            else
                s = obj.ToString();
            return s;
        }

        /// <summary>
        /// 第一位小写 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToCamel(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToLower() + s.Substring(1);
        }

        /// <summary>
        /// 第一位大写
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToPascal(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToUpper() + s.Substring(1);
        }

        /// <summary>
        /// 转全角(SBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        public static string ToSBC(this string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }

        /// <summary>
        /// 转半角(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        public static string ToDBC(this string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }


        /// <summary>  
        /// 把提取的字母变成大写  
        /// </summary>  
        /// <param name="strText">需要转换的字符串</param>  
        /// <returns>转换结果</returns>  
        public static string GetLowerChineseSpell(this  string strText)
        {
            return ChsToPinYinPlugs.GetChineseSpell(strText).ToLower();
        }
        /// <summary>  
        /// 把提取的字母变成大写  
        /// </summary>  
        /// <param name="myChar">需要转换的字符串</param>  
        /// <returns>转换结果</returns>  
        public static string GetUpperChineseSpell(this string strText)
        {
            return ChsToPinYinPlugs.GetChineseSpell(strText).ToUpper();
        }
    }

 

  

 

常用的扩展方法.

标签:

原文地址:http://www.cnblogs.com/sy-ds/p/4462136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!