#region 对象转换处理
2 /// <summary>
3 /// 判断对象是否为Int32类型的数字
4 /// </summary>
5 /// <param name="Expression"></param>
6 /// <returns></returns>
7 public static bool IsNumeric(object expression)
8 {
9 if (expression != null)
10 return IsNumeric(expression.ToString());
11
12 return false;
13
14 }
15
16 /// <summary>
17 /// 判断对象是否为Int32类型的数字
18 /// </summary>
19 /// <param name="Expression"></param>
20 /// <returns></returns>
21 public static bool IsNumeric(string expression)
22 {
23 if (expression != null)
24 {
25 string str = expression;
26 if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
27 {
28 if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
29 return true;
30 }
31 }
32 return false;
33 }
34
35 /// <summary>
36 /// 是否为Double类型
37 /// </summary>
38 /// <param name="expression"></param>
39 /// <returns></returns>
40 public static bool IsDouble(object expression)
41 {
42 if (expression != null)
43 return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
44
45 return false;
46 }
47
48 /// <summary>
49 /// 将字符串转换为数组
50 /// </summary>
51 /// <param name="str">字符串</param>
52 /// <returns>字符串数组</returns>
53 public static string[] GetStrArray(string str)
54 {
55 return str.Split(new char[',']);
56 }
57
58 /// <summary>
59 /// 将数组转换为字符串
60 /// </summary>
61 /// <param name="list">List</param>
62 /// <param name="speater">分隔符</param>
63 /// <returns>String</returns>
64 public static string GetArrayStr(List<string> list, string speater)
65 {
66 StringBuilder sb = new StringBuilder();
67 for (int i = 0; i < list.Count; i++)
68 {
69 if (i == list.Count - 1)
70 {
71 sb.Append(list[i]);
72 }
73 else
74 {
75 sb.Append(list[i]);
76 sb.Append(speater);
77 }
78 }
79 return sb.ToString();
80 }
81
82 /// <summary>
83 /// object型转换为bool型
84 /// </summary>
85 /// <param name="strValue">要转换的字符串</param>
86 /// <param name="defValue">缺省值</param>
87 /// <returns>转换后的bool类型结果</returns>
88 public static bool StrToBool(object expression, bool defValue)
89 {
90 if (expression != null)
91 return StrToBool(expression, defValue);
92
93 return defValue;
94 }
95
96 /// <summary>
97 /// string型转换为bool型
98 /// </summary>
99 /// <param name="strValue">要转换的字符串</param>
100 /// <param name="defValue">缺省值</param>
101 /// <returns>转换后的bool类型结果</returns>
102 public static bool StrToBool(string expression, bool defValue)
103 {
104 if (expression != null)
105 {
106 if (string.Compare(expression, "true", true) == 0)
107 return true;
108 else if (string.Compare(expression, "false", true) == 0)
109 return false;
110 }
111 return defValue;
112 }
113
114 /// <summary>
115 /// 将对象转换为Int32类型
116 /// </summary>
117 /// <param name="expression">要转换的字符串</param>
118 /// <param name="defValue">缺省值</param>
119 /// <returns>转换后的int类型结果</returns>
120 public static int ObjToInt(object expression, int defValue)
121 {
122 if (expression != null)
123 return StrToInt(expression.ToString(), defValue);
124
125 return defValue;
126 }
127
128 /// <summary>
129 /// 将字符串转换为Int32类型
130 /// </summary>
131 /// <param name="expression">要转换的字符串</param>
132 /// <param name="defValue">缺省值</param>
133 /// <returns>转换后的int类型结果</returns>
134 public static int StrToInt(string expression, int defValue)
135 {
136 if (string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 || !Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
137 return defValue;
138
139 int rv;
140 if (Int32.TryParse(expression, out rv))
141 return rv;
142
143 return Convert.ToInt32(StrToFloat(expression, defValue));
144 }
145
146 /// <summary>
147 /// Object型转换为decimal型
148 /// </summary>
149 /// <param name="strValue">要转换的字符串</param>
150 /// <param name="defValue">缺省值</param>
151 /// <returns>转换后的decimal类型结果</returns>
152 public static decimal ObjToDecimal(object expression, decimal defValue)
153 {
154 if (expression != null)
155 return StrToDecimal(expression.ToString(), defValue);
156
157 return defValue;
158 }
159
160 /// <summary>
161 /// string型转换为decimal型
162 /// </summary>
163 /// <param name="strValue">要转换的字符串</param>
164 /// <param name="defValue">缺省值</param>
165 /// <returns>转换后的decimal类型结果</returns>
166 public static decimal StrToDecimal(string expression, decimal defValue)
167 {
168 if ((expression == null) || (expression.Length > 10))
169 return defValue;
170
171 decimal intValue = defValue;
172 if (expression != null)
173 {
174 bool IsDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
175 if (IsDecimal)
176 decimal.TryParse(expression, out intValue);
177 }
178 return intValue;
179 }
180
181 /// <summary>
182 /// Object型转换为float型
183 /// </summary>
184 /// <param name="strValue">要转换的字符串</param>
185 /// <param name="defValue">缺省值</param>
186 /// <returns>转换后的int类型结果</returns>
187 public static float ObjToFloat(object expression, float defValue)
188 {
189 if (expression != null)
190 return StrToFloat(expression.ToString(), defValue);
191
192 return defValue;
193 }
194
195 /// <summary>
196 /// string型转换为float型
197 /// </summary>
198 /// <param name="strValue">要转换的字符串</param>
199 /// <param name="defValue">缺省值</param>
200 /// <returns>转换后的int类型结果</returns>
201 public static float StrToFloat(string expression, float defValue)
202 {
203 if ((expression == null) || (expression.Length > 10))
204 return defValue;
205
206 float intValue = defValue;
207 if (expression != null)
208 {
209 bool IsFloat = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
210 if (IsFloat)
211 float.TryParse(expression, out intValue);
212 }
213 return intValue;
214 }
215
216 /// <summary>
217 /// 将对象转换为日期时间类型
218 /// </summary>
219 /// <param name="str">要转换的字符串</param>
220 /// <param name="defValue">缺省值</param>
221 /// <returns>转换后的int类型结果</returns>
222 public static DateTime StrToDateTime(string str, DateTime defValue)
223 {
224 if (!string.IsNullOrEmpty(str))
225 {
226 DateTime dateTime;
227 if (DateTime.TryParse(str, out dateTime))
228 return dateTime;
229 }
230 return defValue;
231 }
232
233 /// <summary>
234 /// 将对象转换为日期时间类型
235 /// </summary>
236 /// <param name="str">要转换的字符串</param>
237 /// <returns>转换后的int类型结果</returns>
238 public static DateTime StrToDateTime(string str)
239 {
240 return StrToDateTime(str, DateTime.Now);
241 }
242
243 /// <summary>
244 /// 将对象转换为日期时间类型
245 /// </summary>
246 /// <param name="obj">要转换的对象</param>
247 /// <returns>转换后的int类型结果</returns>
248 public static DateTime ObjectToDateTime(object obj)
249 {
250 return StrToDateTime(obj.ToString());
251 }
252
253 /// <summary>
254 /// 将对象转换为日期时间类型
255 /// </summary>
256 /// <param name="obj">要转换的对象</param>
257 /// <param name="defValue">缺省值</param>
258 /// <returns>转换后的int类型结果</returns>
259 public static DateTime ObjectToDateTime(object obj, DateTime defValue)
260 {
261 return StrToDateTime(obj.ToString(), defValue);
262 }
263
264 /// <summary>
265 /// 将对象转换为字符串
266 /// </summary>
267 /// <param name="obj">要转换的对象</param>
268 /// <returns>转换后的string类型结果</returns>
269 public static string ObjectToStr(object obj)
270 {
271 if (obj == null)
272 return "";
273 return obj.ToString().Trim();
274 }
275 #endregion