c# 編程中常用的一些方法

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   for   sp   div   

1.判斷一個字串是否全是數字

        /// <summary>        /// 判斷字串是否全是數字        /// </summary>        /// <param name="str"></param>        /// <returns></returns>        public static bool IsNumber(string str)        {            if (str == null || str.Length == 0)                return false;            char c;            for (int i = 0; i < str.Length; i++)            {                c = str[i];                if (c < ‘0‘ || c > ‘9‘)                                   return false;                           }            return true;        }

2.判斷一個字串是否是手機號

      /// <summary>        /// 判斷一個字串是否是手機號(正確形式:13..9位元字或15...9位元字或18...9位元字)        /// </summary>        /// <param name="str"></param>        /// <returns></returns>        public static bool IsMobilePhone(string str)        {            if (str == null || str.Length == 0)                return false;            string pattern = @"^(?:13\d{1}|15[0-9]|18[0-9])\d{8}$";            if (System.Text.RegularExpressions.Regex.IsMatch(str, pattern) == true)            {                return true;            }            else            {                return false;            }        }

3.判斷一個字串是否是中國的固定電話(正確形式:3-4位元字 - 7-8位元字 010-12345678)

        /// <summary>        /// 判斷一個字串是否是中國的固定電話(正確形式:3-4位元字 - 7-8位元字 010-12345678)        /// </summary>        /// <param name="str"></param>        /// <returns></returns>        public static bool IsTelNumber(string str)        {            if (str == null || str.Length == 0)                return false;            string pattern = @"^(\d{3,4})-(\d{7,8})$";            if (System.Text.RegularExpressions.Regex.IsMatch(str, pattern) == true)            {                return true;            }            else            {                return false;            }        }

4.得到標準格式時間字串

      /// <summary>        /// 得到標準格式時間字串        /// </summary>        /// <returns></returns>        public static string GetStandardDateTime()        {            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");        }

5.把時間字串轉換為標準格式的時間字串

       /// <summary>        /// 把時間字串轉換為標準格式的時間字串        /// </summary>        /// <param name="strDateTime"></param>        /// <returns></returns>        public static string ConvertToStandardDateTime(string strDateTime)        {            try            {                DateTime dt = Convert.ToDateTime(strDateTime);                return dt.ToString("yyyy-MM-dd HH:mm:ss");            }            catch            {                throw new Exception("時間格式不正確!");            }        }

6.將時間字串轉換為時間

        /// <summary>        /// 將時間字串轉換為時間        /// </summary>              public static DateTime ConvertToDateTime(string strDateTime)        {            try            {                return Convert.ToDateTime(strDateTime);            }            catch            {                throw new Exception("時間格式不正確!");            }        }

7.提示框樣式

  public static void MessageWarnning(string message, string caption)        {            MessageBox.Show(message, caption, MessageBoxButton.OK, MessageBoxImage.Warning);        }        public static void MsgError(string message)        {            MessageBox.Show(message, "錯誤資訊", MessageBoxButton.OK, MessageBoxImage.Hand);        }        public static void MsgInfo(string message)        {            MessageBox.Show(message, "提示資訊", MessageBoxButton.OK, MessageBoxImage.Information);        }

 

  

 

c# 編程中常用的一些方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.