zoukankan      html  css  js  c++  java
  • c# 编程中常用的一些方法

    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 = @"^(?:13d{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);
            }

      

  • 相关阅读:
    记录百度编辑器bug(在编辑框输入光标到达页面最底部时,功能区块会悬浮在页面最顶部并且与编辑框分离)
    ThinkPHP5实用的数据库操作方法
    Windows10专业工作站版激活方法
    ThinkPHP5验证码不显示的原因及解决方法
    PHPExcel导出工作蒲(多表合并)教程+详细代码解读
    将input file的选择的文件清空的两种解决方案
    微信公众号网页开发——阻止微信客户端内点击任何图片自动放大
    开发中能快速定位错误也是技术的表现,附上【Chrome开发者工具官方中文文档】
    thinkPHP5 报错session_start(): No session id returned by function解决方法
    Linux安装PHPRedis扩展
  • 原文地址:https://www.cnblogs.com/chengjunwei/p/4039681.html
Copyright © 2011-2022 走看看