博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net基本数据类型操作
阅读量:7168 次
发布时间:2019-06-29

本文共 7219 字,大约阅读时间需要 24 分钟。

     代码编写的过程中,较多的会涉及到基本数据类型的使用和定义,在项目中,对于类型的判定和类型间的相互转换,有时也较为的麻烦,先提供几种基本数据类型的判断方法:

1.判断对象是否为Int32类型的数字:

///         /// 判断对象是否为Int32类型的数字        ///         ///         /// 
public static bool IsNumeric(object expression) { return expression != null && IsNumeric(expression.ToString()); } /// /// 判断对象是否为Int32类型的数字 /// /// ///
public static bool IsNumeric(string expression) { var str = expression; if (!(str.Length > 0) || str.Length > 11 || !System.Text.RegularExpressions.Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$")) return false; return (str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'); }

2.是否为Double类型:

///         /// 是否为Double类型        ///         ///         /// 
public static bool IsDouble(object expression) { if (expression != null) return System.Text.RegularExpressions.Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$"); return false; }

3.将字符串转换为数组:

///         /// 将字符串转换为数组        ///         /// 字符串        /// 
字符串数组
public static string[] GetStrArray(string str) { return str.Split(new char[',']); }

4.将数组转换为字符串:

///         /// 将数组转换为字符串        ///         /// List        /// 分隔符        /// 
String
public static string GetArrayStr(List
list, string speater) { var sb = new StringBuilder(); for (int i = 0; i < list.Count; i++) { if (i == list.Count - 1) { sb.Append(list[i]); } else { sb.Append(list[i]); sb.Append(speater); } } return sb.ToString(); }

5.object型转换为bool型:

///         /// object型转换为bool型        ///         ///         /// 缺省值        /// 
转换后的bool类型结果
public static bool StrToBool(object expression, bool defValue) { if (expression != null) return StrToBool(expression, defValue); return defValue; }

6.string型转换为bool型:

///         /// string型转换为bool型        ///         ///         /// 缺省值        /// 
转换后的bool类型结果
public static bool StrToBool(string expression, bool defValue) { if (expression == null) return defValue; if (string.Compare(expression, "true", StringComparison.OrdinalIgnoreCase) == 0) return true; return string.Compare(expression, "false", StringComparison.OrdinalIgnoreCase) != 0 && defValue; }

7.将对象转换为Int32类型:

///         /// 将对象转换为Int32类型        ///         /// 要转换的字符串        /// 缺省值        /// 
转换后的int类型结果
public static int ObjToInt(object expression, int defValue) { return expression != null ? StrToInt(expression.ToString(), defValue) : defValue; }

8.将字符串转换为Int32类型:

///         /// 将字符串转换为Int32类型        ///         /// 要转换的字符串        /// 缺省值        /// 
转换后的int类型结果
public static int StrToInt(string expression, int defValue) { if (string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 || !System.Text.RegularExpressions.Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$")) return defValue; int rv; if (int.TryParse(expression, out rv)) return rv; return Convert.ToInt32(StrToFloat(expression, defValue)); }

9.Object型转换为decimal型:

///         /// Object型转换为decimal型        ///         ///         /// 缺省值        /// 
转换后的decimal类型结果
public static decimal ObjToDecimal(object expression, decimal defValue) { if (expression != null) return StrToDecimal(expression.ToString(), defValue); return defValue; }

10.string型转换为decimal型:

///         /// string型转换为decimal型        ///         ///         /// 缺省值        /// 
转换后的decimal类型结果
public static decimal StrToDecimal(string expression, decimal defValue) { if ((expression == null) || (expression.Length > 10)) return defValue; decimal intValue = defValue; { bool isDecimal = System.Text.RegularExpressions.Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (isDecimal) decimal.TryParse(expression, out intValue); } return intValue; }

11.Object型转换为float型:

///         /// Object型转换为float型        ///         /// 要转换的字符串        ///         /// 缺省值        /// 
转换后的int类型结果
public static float ObjToFloat(object expression, float defValue) { if (expression != null) return StrToFloat(expression.ToString(), defValue); return defValue; }

12.string型转换为float型:

///         /// string型转换为float型        ///         ///         /// 缺省值        /// 
转换后的int类型结果
public static float StrToFloat(string expression, float defValue) { if ((expression == null) || (expression.Length > 10)) return defValue; float intValue = defValue; { bool isFloat = System.Text.RegularExpressions.Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (isFloat) float.TryParse(expression, out intValue); } return intValue; }

13.将对象转换为日期时间类型(提供几种操作方法份的重载):

///         /// 将对象转换为日期时间类型        ///         /// 要转换的字符串        /// 缺省值        /// 
转换后的int类型结果
public static DateTime StrToDateTime(string str, DateTime defValue) { if (!string.IsNullOrEmpty(str)) { DateTime dateTime; if (DateTime.TryParse(str, out dateTime)) return dateTime; } return defValue; } /// /// 将对象转换为日期时间类型 /// /// 要转换的字符串 ///
转换后的int类型结果
public static DateTime StrToDateTime(string str) { return StrToDateTime(str, DateTime.Now); } /// /// 将对象转换为日期时间类型 /// /// 要转换的对象 ///
转换后的int类型结果
public static DateTime ObjectToDateTime(object obj) { return StrToDateTime(obj.ToString()); } /// /// 将对象转换为日期时间类型 /// /// 要转换的对象 /// 缺省值 ///
转换后的int类型结果
public static DateTime ObjectToDateTime(object obj, DateTime defValue) { return StrToDateTime(obj.ToString(), defValue); }

14.将对象转换为字符串:

///         /// 将对象转换为字符串        ///         /// 要转换的对象        /// 
转换后的string类型结果
public static string ObjectToStr(object obj) { if (obj == null) return ""; return obj.ToString().Trim(); }

 

转载地址:http://wfqwm.baihongyu.com/

你可能感兴趣的文章
高性能的智能日志
查看>>
APScheduler BackgroundScheduler
查看>>
lvs-nat与lvs-dr配置
查看>>
『中级篇』容器的技术概述(二)
查看>>
Apache awstats安装报错解决过程适合初学者
查看>>
Vsftp安装及配置虚拟用户
查看>>
JVM内存区域
查看>>
DNS的视图功能的简单配置。
查看>>
linux和windows互传文件/用户配置文件和密码配置文件/用户组管理/用户管理
查看>>
通过javascript把图片转化为字符画
查看>>
OpenJPA 一些难搞的查询
查看>>
设置button的样式,使得按钮的图片在上面,文字在图片的下面
查看>>
MySQL之函数、存储过程和触发器
查看>>
完整版的OpenLDAP搭建全过程
查看>>
java反射学习总结
查看>>
104. ftl 小数位处理
查看>>
Cannot open /usr/local/varnish/var/varnish/test.localdomain/_.vsm: No such file or directory
查看>>
我的VIM -- vimrc配置
查看>>
Tengine ngx_http_upstream_check_module 健康功能检测使用
查看>>
将数组A中的内容和数组B中的内容进行交换。(数组一样大)
查看>>