1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Net;
7 using System.Web;
8 using System.Security.Cryptography;
9 using System.Text.RegularExpressions;
10 using System.Web.Script.Serialization;
11 using System.Data;
12 using System.Collections;
13 using System.Runtime.Serialization.Json;
14 using System.Configuration;
15 using System.Reflection;
16
17 namespace Common
18 {
19 /// <summary>
20 /// 系统帮助类
21 /// </summary>
22 public class Utils
23 {
24 #region 对象转换处理
25 /// <summary>
26 /// 判断对象是否为Int32类型的数字
27 /// </summary>
28 /// <param name="Expression"></param>
29 /// <returns></returns>
30 public static bool IsNumeric(object expression)
31 {
32 if (expression != null)
33 return IsNumeric(expression.ToString());
34
35 return false;
36
37 }
38
39 /// <summary>
40 /// 判断对象是否为Int32类型的数字
41 /// </summary>
42 /// <param name="Expression"></param>
43 /// <returns></returns>
44 public static bool IsNumeric(string expression)
45 {
46 if (expression != null)
47 {
48 string str = expression;
49 if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
50 {
51 if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
52 return true;
53 }
54 }
55 return false;
56 }
57
58 /// <summary>
59 /// 是否为Double类型
60 /// </summary>
61 /// <param name="expression"></param>
62 /// <returns></returns>
63 public static bool IsDouble(object expression)
64 {
65 if (expression != null)
66 return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
67
68 return false;
69 }
70
71 /// <summary>
72 /// 将字符串转换为数组
73 /// </summary>
74 /// <param name="str">字符串</param>
75 /// <returns>字符串数组</returns>
76 public static string[] GetStrArray(string str)
77 {
78 return str.Split(new char[',']);
79 }
80
81 /// <summary>
82 /// 将数组转换为字符串
83 /// </summary>
84 /// <param name="list">List</param>
85 /// <param name="speater">分隔符</param>
86 /// <returns>String</returns>
87 public static string GetArrayStr(List<string> list, string speater)
88 {
89 StringBuilder sb = new StringBuilder();
90 for (int i = 0; i < list.Count; i++)
91 {
92 if (i == list.Count - 1)
93 {
94 sb.Append(list[i]);
95 }
96 else
97 {
98 sb.Append(list[i]);
99 sb.Append(speater);
100 }
101 }
102 return sb.ToString();
103 }
104
105 /// <summary>
106 /// object型转换为bool型
107 /// </summary>
108 /// <param name="strValue">要转换的字符串</param>
109 /// <param name="defValue">缺省值</param>
110 /// <returns>转换后的bool类型结果</returns>
111 public static bool StrToBool(object expression, bool defValue)
112 {
113 if (expression != null)
114 return StrToBool(expression, defValue);
115
116 return defValue;
117 }
118
119 /// <summary>
120 /// string型转换为bool型
121 /// </summary>
122 /// <param name="strValue">要转换的字符串</param>
123 /// <param name="defValue">缺省值</param>
124 /// <returns>转换后的bool类型结果</returns>
125 public static bool StrToBool(string expression, bool defValue)
126 {
127 if (expression != null)
128 {
129 if (string.Compare(expression, "true", true) == 0)
130 return true;
131 else if (string.Compare(expression, "false", true) == 0)
132 return false;
133 }
134 return defValue;
135 }
136
137 /// <summary>
138 /// 将对象转换为Int32类型
139 /// </summary>
140 /// <param name="expression">要转换的字符串</param>
141 /// <param name="defValue">缺省值</param>
142 /// <returns>转换后的int类型结果</returns>
143 public static int ObjToInt(object expression, int defValue)
144 {
145 if (expression != null)
146 return StrToInt(expression.ToString(), defValue);
147
148 return defValue;
149 }
150
151 /// <summary>
152 /// 将字符串转换为Int32类型
153 /// </summary>
154 /// <param name="expression">要转换的字符串</param>
155 /// <param name="defValue">缺省值</param>
156 /// <returns>转换后的int类型结果</returns>
157 public static int StrToInt(string expression, int defValue)
158 {
159 if (string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 || !Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
160 return defValue;
161
162 int rv;
163 if (Int32.TryParse(expression, out rv))
164 return rv;
165
166 return Convert.ToInt32(StrToFloat(expression, defValue));
167 }
168
169 /// <summary>
170 /// Object型转换为decimal型
171 /// </summary>
172 /// <param name="strValue">要转换的字符串</param>
173 /// <param name="defValue">缺省值</param>
174 /// <returns>转换后的decimal类型结果</returns>
175 public static decimal ObjToDecimal(object expression, decimal defValue)
176 {
177 if (expression != null)
178 return StrToDecimal(expression.ToString(), defValue);
179
180 return defValue;
181 }
182
183 /// <summary>
184 /// string型转换为decimal型
185 /// </summary>
186 /// <param name="strValue">要转换的字符串</param>
187 /// <param name="defValue">缺省值</param>
188 /// <returns>转换后的decimal类型结果</returns>
189 public static decimal StrToDecimal(string expression, decimal defValue)
190 {
191 if ((expression == null) || (expression.Length > 10))
192 return defValue;
193
194 decimal intValue = defValue;
195 if (expression != null)
196 {
197 bool IsDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
198 if (IsDecimal)
199 decimal.TryParse(expression, out intValue);
200 }
201 return intValue;
202 }
203
204 /// <summary>
205 /// Object型转换为float型
206 /// </summary>
207 /// <param name="strValue">要转换的字符串</param>
208 /// <param name="defValue">缺省值</param>
209 /// <returns>转换后的int类型结果</returns>
210 public static float ObjToFloat(object expression, float defValue)
211 {
212 if (expression != null)
213 return StrToFloat(expression.ToString(), defValue);
214
215 return defValue;
216 }
217
218 /// <summary>
219 /// string型转换为float型
220 /// </summary>
221 /// <param name="strValue">要转换的字符串</param>
222 /// <param name="defValue">缺省值</param>
223 /// <returns>转换后的int类型结果</returns>
224 public static float StrToFloat(string expression, float defValue)
225 {
226 if ((expression == null) || (expression.Length > 10))
227 return defValue;
228
229 float intValue = defValue;
230 if (expression != null)
231 {
232 bool IsFloat = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
233 if (IsFloat)
234 float.TryParse(expression, out intValue);
235 }
236 return intValue;
237 }
238
239 /// <summary>
240 /// 将对象转换为日期时间类型
241 /// </summary>
242 /// <param name="str">要转换的字符串</param>
243 /// <param name="defValue">缺省值</param>
244 /// <returns>转换后的int类型结果</returns>
245 public static DateTime StrToDateTime(string str, DateTime defValue)
246 {
247 if (!string.IsNullOrEmpty(str))
248 {
249 DateTime dateTime;
250 if (DateTime.TryParse(str, out dateTime))
251 return dateTime;
252 }
253 return defValue;
254 }
255
256 /// <summary>
257 /// 将对象转换为日期时间类型
258 /// </summary>
259 /// <param name="str">要转换的字符串</param>
260 /// <returns>转换后的int类型结果</returns>
261 public static DateTime StrToDateTime(string str)
262 {
263 return StrToDateTime(str, DateTime.Now);
264 }
265
266 /// <summary>
267 /// 将对象转换为日期时间类型
268 /// </summary>
269 /// <param name="obj">要转换的对象</param>
270 /// <returns>转换后的int类型结果</returns>
271 public static DateTime ObjectToDateTime(object obj)
272 {
273 return StrToDateTime(obj.ToString());
274 }
275
276 /// <summary>
277 /// 将对象转换为日期时间类型
278 /// </summary>
279 /// <param name="obj">要转换的对象</param>
280 /// <param name="defValue">缺省值</param>
281 /// <returns>转换后的int类型结果</returns>
282 public static DateTime ObjectToDateTime(object obj, DateTime defValue)
283 {
284 return StrToDateTime(obj.ToString(), defValue);
285 }
286
287 /// <summary>
288 /// 将对象转换为字符串
289 /// </summary>
290 /// <param name="obj">要转换的对象</param>
291 /// <returns>转换后的string类型结果</returns>
292 public static string ObjectToStr(object obj)
293 {
294 if (obj == null)
295 return "";
296 return obj.ToString().Trim();
297 }
298 #endregion
299
300 #region 分割字符串
301 /// <summary>
302 /// 分割字符串
303 /// </summary>
304 public static string[] SplitString(string strContent, string strSplit)
305 {
306 if (!string.IsNullOrEmpty(strContent))
307 {
308 if (strContent.IndexOf(strSplit) < 0)
309 return new string[] { strContent };
310
311 return Regex.Split(strContent, Regex.Escape(strSplit), RegexOptions.IgnoreCase);
312 }
313 else
314 return new string[0] { };
315 }
316
317 /// <summary>
318 /// 分割字符串
319 /// </summary>
320 /// <returns></returns>
321 public static string[] SplitString(string strContent, string strSplit, int count)
322 {
323 string[] result = new string[count];
324 string[] splited = SplitString(strContent, strSplit);
325
326 for (int i = 0; i < count; i++)
327 {
328 if (i < splited.Length)
329 result[i] = splited[i];
330 else
331 result[i] = string.Empty;
332 }
333
334 return result;
335 }
336 #endregion
337
338 #region 截取字符串
339 public static string GetSubString(string p_SrcString, int p_Length, string p_TailString)
340 {
341 return GetSubString(p_SrcString, 0, p_Length, p_TailString);
342 }
343 public static string GetSubString(string p_SrcString, int p_StartIndex, int p_Length, string p_TailString)
344 {
345 string str = p_SrcString;
346 byte[] bytes = Encoding.UTF8.GetBytes(p_SrcString);
347 foreach (char ch in Encoding.UTF8.GetChars(bytes))
348 {
349 if (((ch > 'ࠀ') && (ch < '一')) || ((ch > 0xac00) && (ch < 0xd7a3)))
350 {
351 if (p_StartIndex >= p_SrcString.Length)
352 {
353 return "";
354 }
355 return p_SrcString.Substring(p_StartIndex, ((p_Length + p_StartIndex) > p_SrcString.Length) ? (p_SrcString.Length - p_StartIndex) : p_Length);
356 }
357 }
358 if (p_Length < 0)
359 {
360 return str;
361 }
362 byte[] sourceArray = Encoding.Default.GetBytes(p_SrcString);
363 if (sourceArray.Length <= p_StartIndex)
364 {
365 return str;
366 }
367 int length = sourceArray.Length;
368 if (sourceArray.Length > (p_StartIndex + p_Length))
369 {
370 length = p_Length + p_StartIndex;
371 }
372 else
373 {
374 p_Length = sourceArray.Length - p_StartIndex;
375 p_TailString = "";
376 }
377 int num2 = p_Length;
378 int[] numArray = new int[p_Length];
379 byte[] destinationArray = null;
380 int num3 = 0;
381 for (int i = p_StartIndex; i < length; i++)
382 {
383 if (sourceArray[i] > 0x7f)
384 {
385 num3++;
386 if (num3 == 3)
387 {
388 num3 = 1;
389 }
390 }
391 else
392 {
393 num3 = 0;
394 }
395 numArray[i] = num3;
396 }
397 if ((sourceArray[length - 1] > 0x7f) && (numArray[p_Length - 1] == 1))
398 {
399 num2 = p_Length + 1;
400 }
401 destinationArray = new byte[num2];
402 Array.Copy(sourceArray, p_StartIndex, destinationArray, 0, num2);
403 return (Encoding.Default.GetString(destinationArray) + p_TailString);
404 }
405 #endregion
406
407 #region 删除最后结尾的一个逗号
408 /// <summary>
409 /// 删除最后结尾的一个逗号
410 /// </summary>
411 public static string DelLastComma(string str)
412 {
413 return str.Substring(0, str.LastIndexOf(","));
414 }
415 #endregion
416
417 #region 删除最后结尾的指定字符后的字符
418 /// <summary>
419 /// 删除最后结尾的指定字符后的字符
420 /// </summary>
421 public static string DelLastChar(string str, string strchar)
422 {
423 if (string.IsNullOrEmpty(str))
424 return "";
425 if (str.LastIndexOf(strchar) >= 0 && str.LastIndexOf(strchar) == str.Length - 1)
426 {
427 return str.Substring(0, str.LastIndexOf(strchar));
428 }
429 return str;
430 }
431 #endregion
432
433 #region 生成指定长度的字符串
434 /// <summary>
435 /// 生成指定长度的字符串,即生成strLong个str字符串
436 /// </summary>
437 /// <param name="strLong">生成的长度</param>
438 /// <param name="str">以str生成字符串</param>
439 /// <returns></returns>
440 public static string StringOfChar(int strLong, string str)
441 {
442 string ReturnStr = "";
443 for (int i = 0; i < strLong; i++)
444 {
445 ReturnStr += str;
446 }
447
448 return ReturnStr;
449 }
450 #endregion
451
452 #region 生成日期随机码
453 /// <summary>
454 /// 生成日期随机码
455 /// </summary>
456 /// <returns></returns>
457 public static string GetRamCode()
458 {
459 #region
460 return DateTime.Now.ToString("yyyyMMddHHmmssffff");
461 #endregion
462 }
463 #endregion
464
465 #region 生成随机字母或数字
466 /// <summary>
467 /// 生成随机数字
468 /// </summary>
469 /// <param name="length">生成长度</param>
470 /// <returns></returns>
471 public static string Number(int Length)
472 {
473 return Number(Length, false);
474 }
475
476 /// <summary>
477 /// 生成随机数字
478 /// </summary>
479 /// <param name="Length">生成长度</param>
480 /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
481 /// <returns></returns>
482 public static string Number(int Length, bool Sleep)
483 {
484 if (Sleep)
485 System.Threading.Thread.Sleep(3);
486 string result = "";
487 System.Random random = new Random();
488 for (int i = 0; i < Length; i++)
489 {
490 result += random.Next(10).ToString();
491 }
492 return result;
493 }