字符串、文件大小格式、气泡信息提示、邮箱地址和电话号码合法性

        /// <summary>
        /// 判断字符串是否为整数
         /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumeric(string str)
        {
            if (str.Trim() == "")
            {
                return false;
            }
            foreach (char ch in str)
            {
                if (ch < 48 || ch > 57)
                    return false;
            }
            return true;
        }

        /// <summary>
        /// 把文件大小转为可读的字符串
         /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static string FileSizeToReadString(long size)
        {
            StringBuilder sb = new StringBuilder();
            string strUnits = "Bytes";
            float fSize = 0.0F;
            if (size > 1024)
            {
                if (size < 1024 * 1024)
                {
                    strUnits = "KB";
                    fSize = Convert.ToSingle(size) / 1024;
                }
                else
                {
                    strUnits = "MB";
                    fSize = Convert.ToSingle(size) / 1048576;
                }
                sb.AppendFormat("{0:0.0} {1}", fSize, strUnits);
            }
            else
            {
                fSize = Convert.ToSingle(size);
                sb.AppendFormat("{0:0} {1}", fSize, strUnits);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 文件大小格式
         /// </summary>
        /// <param name="size">文件字节数(bytes)</param>
        /// <returns></returns>
        private string FormatFileSize(long size)
        {
            int[] limits = new int[] { 1024 * 1024 * 1024, 1024 * 1024, 1024 };
            string[] units = new string[] { "GB", "MB", "KB" };
            for (int i = 0; i < limits.Length; i++)
            {
                if (size >= limits[i])
                    return String.Format("{0:#,##0.##} " + units[i], ((double)size / limits[i]));
            }
            return String.Format("{0} bytes", size);
        }

        /// <summary>
        /// 显示气泡提示信息
         /// </summary>
        /// <param name="text"></param>
        /// <param name="win"></param>
        public static void ShowMessage(string text, IWin32Window win)
        {
            ToolTip toolTipMsg = new ToolTip();
            if (toolTipMsg == null)
            {
                toolTipMsg = new ToolTip();
                toolTipMsg.IsBalloon = true;
                toolTipMsg.ToolTipIcon = ToolTipIcon.Info;
                toolTipMsg.ToolTipTitle = "提示";
            }
            toolTipMsg.Hide(win);
            toolTipMsg.Show(text, win, 0, -60, 3000);
        }

        /// <summary>
        /// 显示气泡提示信息
         /// </summary>
        /// <param name="text"></param>
        /// <param name="win"></param>
        public static void ShowMessage(string text, IWin32Window win, int x, int y)
        {
            ToolTip toolTipMsg = new ToolTip();
            if (toolTipMsg == null)
            {
                toolTipMsg = new ToolTip();
                toolTipMsg.IsBalloon = true;
                toolTipMsg.ToolTipIcon = ToolTipIcon.Info;
                toolTipMsg.ToolTipTitle = "提示";
            }
            toolTipMsg.Hide(win);
            toolTipMsg.Show(text, win, x, y - 60, 1000);
        }

        public static void ShowMessage(string text, IWin32Window win, int x, int y, int time)
        {
            ToolTip toolTipMsg = new ToolTip();
            if (toolTipMsg == null)
            {
                toolTipMsg = new ToolTip();
                toolTipMsg.IsBalloon = true;
                toolTipMsg.ToolTipIcon = ToolTipIcon.Info;
                toolTipMsg.ToolTipTitle = "提示";
            }
            toolTipMsg.Hide(win);
            toolTipMsg.Show(text, win, x, y - 60, time);
        }

        /// <summary>
        /// 显示详细信息提示
         /// </summary>
        /// <param name="text"></param>
        /// <param name="win"></param>
        public static void ShowMessageEx(string text, Control cl)
        {
            ToolTip toolTipMsg = new ToolTip();
            if (toolTipMsg == null)
            {
                toolTipMsg = new ToolTip();
                toolTipMsg.IsBalloon = true;
                toolTipMsg.SetToolTip(cl, text);
            }
        }

        /// <summary>
        /// 去掉重复项
         /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DelRepeat(string str)
        {
            string[] values = str.Split(';');
            List<string> list = new List<string>();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < values.Length; i++)
            {
                if (values[i] != string.Empty && list.IndexOf(values[i]) == -1)
                {
                    list.Add(values[i]);
                    sb.Append(values[i]);
                    sb.Append(";");
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 判断邮件地址是否合法
         /// </summary>
        /// <param name="inputEmail"></param>
        /// <returns></returns>
        public static bool isEmail(string inputEmail, out string errEmail)
        {
            string strRegex = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            Regex re = new Regex(strRegex);
            string[] emails = inputEmail.Split(';');
            foreach (string email in emails)
            {
                if (!re.IsMatch(email))
                {
                    errEmail = email;
                    return false;
                }
            }
            errEmail = "";
            return true;
        }

        /// <summary>
        /// 判断邮件地址是否合法
         /// </summary>
        /// <param name="inputEmail"></param>
        /// <returns></returns>
        public static bool isEmail(string email)
        {
            string strRegex = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            Regex re = new Regex(strRegex);
            if (!re.IsMatch(email))
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 判断是否是电话号码
         /// </summary>
        /// <param name="phoneNumber"></param>
        /// <param name="errorNumber"></param>
        /// <returns></returns>
        public static bool isPhoneNumber(string phoneNumber, out string errorNumber)
        {
            string strRegex1 = @"^1(([0-1][0-9])|(2[0-5])|(3[0-9])|(5[^4])|(8[5-9])|(80))\d{8}$";
            string strRegex0 = @"^0\d{10,11}$";
            Regex re1 = new Regex(strRegex1);
            Regex re0 = new Regex(strRegex0);
            string[] phones = phoneNumber.Split(';');
            foreach (string phone in phones)
            {
                if (!(re0.IsMatch(phone) | re1.IsMatch(phone)))
                {
                    errorNumber = phone;
                    return false;
                }
            }
            errorNumber = "";
            return true;
        }

        /// <summary>
        /// 判断是否是电话号码
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        public static bool isPhoneNumber(string phoneNumber)
        {
            string strRegex1 = @"^1(([0-1][0-9])|(2[0-5])|(3[0-9])|(5[^4])|(8[5-9])|(80))\d{8}$";
            string strRegex0 = @"^0\d{10,11}$";
            Regex re1 = new Regex(strRegex1);
            Regex re0 = new Regex(strRegex0);
            if (!(re0.IsMatch(phoneNumber) | re1.IsMatch(phoneNumber)))
            {
                return false;
            }
            return true;
        }

 

posted @ 2013-06-23 12:49  LS庆  阅读(483)  评论(0编辑  收藏  举报