首页 / C# / c# 编程中常用的一些方法
c# 编程中常用的一些方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# 编程中常用的一些方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2594字,纯文字阅读大概需要4分钟。
内容图文
1.判断一个字符串是否全是数字
/// <summary> /// 判断字符串是否全是数字 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsNumber(string str) { if (str == null || str.Length == 0) returnfalse; char c; for (int i = 0; i < str.Length; i++) { c = str[i]; if (c < ‘0‘ || c > ‘9‘) returnfalse; } returntrue; }
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) returnfalse; string pattern = @"^(?:13\d{1}|15[0-9]|18[0-9])\d{8}$"; if (System.Text.RegularExpressions.Regex.IsMatch(str, pattern) == true) { returntrue; } else { returnfalse; } }
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) returnfalse; string pattern = @"^(\d{3,4})-(\d{7,8})$"; if (System.Text.RegularExpressions.Regex.IsMatch(str, pattern) == true) { returntrue; } else { returnfalse; } }
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 { thrownew Exception("时间格式不正确!"); } }
6.将时间字符串转换为时间
/// <summary> /// 将时间字符串转换为时间 /// </summary> public static DateTime ConvertToDateTime(string strDateTime) { try { return Convert.ToDateTime(strDateTime); } catch { thrownew Exception("时间格式不正确!"); } }
7.提示框样式
public static void MessageWarnning(string message, string caption) { MessageBox.Show(message, caption, MessageBoxButton.OK, MessageBoxImage.Warning); } publicstaticvoid MsgError(string message) { MessageBox.Show(message, "错误信息", MessageBoxButton.OK, MessageBoxImage.Hand); } publicstaticvoid MsgInfo(string message) { MessageBox.Show(message, "提示信息", MessageBoxButton.OK, MessageBoxImage.Information); }
原文:http://www.cnblogs.com/chengjunwei/p/4039681.html
内容总结
以上是互联网集市为您收集整理的c# 编程中常用的一些方法全部内容,希望文章能够帮你解决c# 编程中常用的一些方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。