编程浪子的博客

碌碌无为终为过

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

这几天一直在看他写的代码。有一些代码感觉挺有用的,贴出来大家分享。

下边代码都是从他的龙套类(Utils.cs)里边找出来的。

/// <summary>
/// 返回字符串真实长度, 1个汉字长度为2
/// </summary>
/// <returns>字符长度</returns>
public static int GetStringLength(string str)
{
    return Encoding.Default.GetBytes(str).Length;
}

 

/// <summary>
       /// 判断指定字符串在指定字符串数组中的位置
       /// </summary>
       /// <param name="strSearch">字符串</param>
       /// <param name="stringArray">字符串数组</param>
       /// <param name="caseInsensetive">是否不区分大小写, true为不区分, false为区分</param>
       /// <returns>字符串在指定字符串数组中的位置, 如不存在则返回-1</returns>
       public static int GetInArrayID(string strSearch, string[] stringArray, bool caseInsensetive)
       {
           for (int i = 0; i < stringArray.Length; i++)
           {
               if (caseInsensetive)
               {
                   if (strSearch.ToLower() == stringArray[i].ToLower())
                       return i;
               }
               else if (strSearch == stringArray[i])
                   return i;
           }
           return -1;
       }

 

/// <summary>
        /// MD5函数
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <returns>MD5结果</returns>
        public static string MD5(string str)
        {
            byte[] b = Encoding.Default.GetBytes(str);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
                ret += b[i].ToString("x").PadLeft(2, '0');

            return ret;
        }

 

/// <summary>
       /// SHA256函数
       /// </summary>
       /// /// <param name="str">原始字符串</param>
       /// <returns>SHA256结果</returns>
       public static string SHA256(string str)
       {
           byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
           SHA256Managed Sha256 = new SHA256Managed();
           byte[] Result = Sha256.ComputeHash(SHA256Data);
           return Convert.ToBase64String(Result);  //返回长度为44字节的字符串
       }

 

/// <summary>
/// 根据阿拉伯数字返回月份的名称(可更改为某种语言)
/// </summary>   
public static string[] Monthes
{
    get
    {
        return new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    }
}

 

/// <summary>
        /// 清除字符串数组中的重复项
        /// </summary>
        /// <param name="strArray">字符串数组</param>
        /// <param name="maxElementLength">字符串数组中单个元素的最大长度</param>
        /// <returns></returns>
        public static string[] DistinctStringArray(string[] strArray, int maxElementLength)
        {
            Hashtable h = new Hashtable();

            foreach (string s in strArray)
            {
                string k = s;
                if (maxElementLength > 0 && k.Length > maxElementLength)
                {
                    k = k.Substring(0, maxElementLength);
                }
                h[k.Trim()] = s;
            }

            string[] result = new string[h.Count];

            h.Keys.CopyTo(result, 0);

            return result;
        }

/// <summary>
        /// 格式化字节数字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string FormatBytesStr(int bytes)
        {
            if (bytes > 1073741824)
                return ((double)(bytes / 1073741824)).ToString("0") + "G";

            if (bytes > 1048576)
                return ((double)(bytes / 1048576)).ToString("0") + "M";

            if (bytes > 1024)
                return ((double)(bytes / 1024)).ToString("0") + "K";

            return bytes.ToString() + "Bytes";
        }

/// <summary>
/// 是否为ip
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool IsIP(string ip)
{
    return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}

 

/// <summary>
       /// 将全角数字转换为数字
       /// </summary>
       /// <param name="SBCCase"></param>
       /// <returns></returns>
       public static string SBCCaseToNumberic(string SBCCase)
       {
           char[] c = SBCCase.ToCharArray();
           for (int i = 0; i < c.Length; i++)
           {
               byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
               if (b.Length == 2)
               {
                   if (b[1] == 255)
                   {
                       b[0] = (byte)(b[0] + 32);
                       b[1] = 0;
                       c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
                   }
               }
           }
           return new string(c);
       }

 

       /// <summary>
       /// 将字符串转换为Color
       /// </summary>
       /// <param name="color"></param>
       /// <returns></returns>
       public static Color ToColor(string color)
       {
           int red, green, blue = 0;
           char[] rgb;
           color = color.TrimStart('#');
           color = Regex.Replace(color.ToLower(), "[g-zG-Z]", "");
           switch (color.Length)
           {
               case 3:
                   rgb = color.ToCharArray();
                   red = Convert.ToInt32(rgb[0].ToString() + rgb[0].ToString(), 16);
                   green = Convert.ToInt32(rgb[1].ToString() + rgb[1].ToString(), 16);
                   blue = Convert.ToInt32(rgb[2].ToString() + rgb[2].ToString(), 16);
                   return Color.FromArgb(red, green, blue);
               case 6:
                   rgb = color.ToCharArray();
                   red = Convert.ToInt32(rgb[0].ToString() + rgb[1].ToString(), 16);
                   green = Convert.ToInt32(rgb[2].ToString() + rgb[3].ToString(), 16);
                   blue = Convert.ToInt32(rgb[4].ToString() + rgb[5].ToString(), 16);
                   return Color.FromArgb(red, green, blue);
               default:
                   return Color.FromName(color);

           }
       }

posted on 2010-02-05 22:35  编程浪子_  阅读(544)  评论(1编辑  收藏  举报