字符串的扩展方法

public static class ExpandStringClass {
        /// <summary>
        /// 返回无空值的字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetRightString(this object str)
        {
          string temp = string.IsNullOrEmpty(Convert.ToString(str))?(""):(str.ToString());
          return temp;
        }
        /// <summary>
        /// 返回字符串是否为空
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }
        /// <summary>
        /// 将字符串转成数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ToInt(this string str) {
            int temp = 0;
            int.TryParse(str,out temp);
            return temp;
        }
        /// <summary>
        /// 将object转成数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ToInt(this object str)
        {
            int temp = 0;
            int.TryParse(str.ToString(), out temp);
            return temp;
        }

 

        /// <summary>
        /// 将字符串转成小数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static float ToFloat(this string str)
        {
            float temp = 0;
            float.TryParse(str, out temp);
            return temp;
        }
        /// <summary>
        /// 将object转成数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static float ToFloat(this object str)
        {
            float temp = 0;
            float.TryParse(str.ToString(), out temp);
            return temp;
        }


        /// <summary>
        /// 将字符串转成TDouble
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static double ToDouble(this string str)
        {
            double temp = 0;
            double.TryParse(str, out temp);
            return temp;
        }
        /// <summary>
        /// 将object转成TDouble
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static double ToDouble(this object str)
        {
            double temp = 0;
            double.TryParse(str.ToString(), out temp);
            return temp;
        }

        /// <summary>
        /// 将字符串转成数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static long ToLong(this string str)
        {
            long temp = 0;
            long.TryParse(str, out temp);
            return temp;
        }
        /// <summary>
        /// 将object转成数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static long ToLong(this object str)
        {
            long temp = 0;
            long.TryParse(str.ToString(), out temp);
            return temp;
        }


        /// <summary>
        /// 将字符串转成日期
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime ToDate(this string str)
        {
            DateTime temp = DateTime.Now;
            DateTime.TryParse(str, out temp);
            return temp;
        }
        /// <summary>
        /// 将object转成数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime ToDate(this object str)
        {
            DateTime temp = DateTime.Now;
            DateTime.TryParse(str.ToString(), out temp);
            return temp;
        }

        /// <summary>
        /// 将字符串转成布尔
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ToBoolen(this string str)
        {
            bool temp = false;
            bool.TryParse(str, out temp);
            return temp;
        }
        /// <summary>
        /// 将object转成布尔
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ToBoolen(this object str)
        {
            bool temp = false;
            bool.TryParse(str.ToString(), out temp);
            return temp;
        }


        /// <summary>
        /// 格式字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string FormatStr(this string str,params string[] data)
        {
            return String.Format(str,data);
        }
        public static string GetSpell(this string cn)
        {
            string tempChar = "";
            char[] temp = cn.ToCharArray();
            for (int j = 0; j < temp.Length; j++)
            {
                byte[] arrCN = System.Text.Encoding.Default.GetBytes(temp[j].ToString());
                if (arrCN.Length > 1)
                {
                    //&& System.Text.RegularExpressions.Regex.IsMatch(temp[j].ToString(),"^[\\D]*$")
                    int area = (short)arrCN[0];
                    int pos = (short)arrCN[1];
                    int code = (area << 8) + pos;
                    int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
                    for (int i = 0; i < 26; i++)
                    {
                        int max = 55290;
                        if (i != 25) max = areacode[i + 1];
                        if (areacode[i] <= code && code < max)
                        {
                            tempChar += System.Text.Encoding.Default.GetString(new byte[] { (byte)(65 + i) }).ToLower();
                        }
                    }

                }
                else
                    tempChar += temp[j];
            }
            return tempChar;
        }
      
    }

你可能感兴趣的:(字符串)