username@email.com
2021-09-22 1bfc8f9c7baad9be7fdb92742cf796f9fdb50df3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using DealMvc.Common.Net;
 
namespace CY.Infrastructure.Common
{
    /// <summary>
    /// 数据类型转换类
    /// </summary>
    public class MyConvert
    {
        #region string数据转换
        public static bool GetBoolean(string value)
        {
            bool result = false;
            bool.TryParse(value, out result);
            return result;
        }
 
        public static DateTime GetDateTime(string value)
        {
            DateTime now = DateTime.Now;
            DateTime.TryParse(value, out now);
            return now;
        }
 
        public static decimal GetDecimal(string num)
        {
            decimal result = 0M;
            decimal.TryParse(num, out result);
            return result;
        }
 
        public static double GetDouble(string num)
        {
            double result = 0.0;
            double.TryParse(num, out result);
            return result;
        }
 
        public static float GetFloat(string num)
        {
            float result = 0f;
            float.TryParse(num, out result);
            return result;
        }
 
        public static short GetInt16(string num)
        {
            short result = 0;
            short.TryParse(num, out result);
            return result;
        }
 
        public static int GetInt32(string num)
        {
            int result = 0;
            int.TryParse(num, out result);
            return result;
        }
 
        public static long GetInt64(string num)
        {
            long result = 0L;
            long.TryParse(num, out result);
            return result;
        }
 
        #endregion
 
        #region object数据转换
 
 
 
        public static int? ConvertToInt(object targetValue)
        {
            int? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
 
            int result = 0;
            int.TryParse(targetValue.ToString(), out result);
            returnValue = result;
 
            return returnValue;
        }
 
        public static Int32? ConvertToInt32(object targetValue)
        {
            Int32? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
            else { }
 
            if (TypeParse.IsNumeric(targetValue))
                return Int32.Parse(targetValue.ToString());
            else
                return null;
 
        }
 
        public static Int64? ConvertToInt64(object targetValue)
        {
            Int64? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
 
            Int64 result = 0;
            Int64.TryParse(targetValue.ToString(), out result);
            returnValue = result;
 
            return returnValue;
        }
 
        public static long? ConvertToLong(object targetValue)
        {
            long? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
 
            long result = 0;
            long.TryParse(targetValue.ToString(), out result);
            returnValue = result;
 
            return returnValue;
        }
 
        public static Boolean ConvertIntToBoolean(object targetValue)
        {
            return targetValue != DBNull.Value ? ("1".Equals(targetValue.ToString()) || true.ToString().Equals(targetValue.ToString())) : false;
        }
        /// <summary>
        /// 转为Boolean
        /// 2013-5-22 10:00修改实现为:true\"true"\"TRUE"\1 均为true
        /// </summary>
        /// <param name="targetValue"></param>
        /// <returns></returns>
        public static Boolean ConvertToBoolean(object targetValue)
        {
            return targetValue != DBNull.Value ? (true.ToString().ToUpper().Equals(targetValue.ToString().ToUpper()) || "1".Equals(targetValue)) : false;
        }
 
        public static Double? ConvertToDouble(object targetValue)
        {
            Double? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
 
            Double result = 0;
            Double.TryParse(targetValue.ToString(), out result);
            returnValue = result;
 
            return returnValue;
        }
 
        public static decimal? ConvertToDecimal(object targetValue)
        {
            decimal? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
 
            decimal result = 0;
            decimal.TryParse(targetValue.ToString(), out result);
            returnValue = result;
 
            return returnValue;
        }
 
        public static DateTime? ConvertToDateTime(object targetValue)
        {
 
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return null;
            }
            else { }
 
            string targetValueStr = targetValue.ToString();
 
            if (string.IsNullOrEmpty(targetValueStr)) { return null; } else { }
            
            DateTime returnValue;
            DateTime.TryParse(targetValueStr, out returnValue);
 
            return returnValue;
        }
 
 
        public static byte[] ConvertToByte(object targetValue)
        {
            return targetValue != DBNull.Value ? targetValue as byte[] : null;
        }
 
        public static Guid ConvertToGuid(object targetValue)
        {
            Guid outGuid = Guid.Empty;
            Guid.TryParse(string.Format("{0}", targetValue), out outGuid);
            return outGuid;
 
        }
        public static string ConvertToString(object targetValue)
        {
            return string.Format("{0}", targetValue);
        }
        #endregion
    }
 
    public static class StaticFunction
    {
        #region 常用方法
        /// <summary>
        /// object转GUID
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static Guid ToGuid2(this object obj)
        {
            Guid Val;
            Val = MyConvert.ConvertToGuid(obj);
            return Val;
        }
 
 
        /// <summary>
        /// object转String,如何object为null返回Empty
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static string ToString2(this object obj)
        {
            //string Val = string.Empty;
            //try { Val = obj.ToString(); }
            //catch { Val = ""; }
            return obj == null ? string.Empty : obj.ToString();
        }
        /// <summary>
        /// object转Int,失败返回0
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static int? ToInt32(this object obj)
        {
            int? Val = 0;
            if (null == obj || obj == DBNull.Value)
            {
                return null;
            }
            else
            {
                string objxx = obj.ToString();
                if (objxx.IndexOf(".") >= 0)
                {
                    obj = obj.ToString().Split('.')[0];
                }
            }
            Val = MyConvert.ConvertToInt32(obj);
            return Val;
        }
        /// <summary>
        /// object转Double,失败返回0
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static double? ToDouble2(this object obj)
        {
            double? Val = 0;
            Val = MyConvert.ConvertToDouble(obj);
            return Val;
        }
 
 
        /// <summary>
        /// object转Decimal,失败返回0
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static decimal? ToDecimal2(this object obj)
        {
            decimal? Val = 0;
            Val = MyConvert.ConvertToDecimal(obj);
            return Val;
        }
 
        /// <summary>
        /// object转Double,失败返回0
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static string ToDecimal2Yen(this object obj)
        {
            decimal? Val = obj.ToDecimal2();
            if (Val.HasValue)
            {
                return Val.Value.ToString("0.00");
            }
            else
            {
                return "0.00";
            }
        }
 
        /// <summary>
        /// object转DateTime
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static DateTime? ToDateTime2(this object obj)
        {
            return MyConvert.ConvertToDateTime(obj);
        }
 
        /// <summary>
        /// DateTime?格式化,如何DateTime?为null返回Empty
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string ToStringForDateTime(this DateTime? dt, string format)
        {
            string val = string.Empty;
            try
            {
                val = ((DateTime)dt).ToString(format, System.Globalization.DateTimeFormatInfo.InvariantInfo);
            }
            catch
            {
            }
            return val;
        }
        /// <summary>
        /// object转Boolean,失败返回flase
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public static bool ToBoolean2(this object obj)
        {
            bool Val = false;
            try
            {
                Val = Convert.ToBoolean(obj);
            }
            catch
            {
                Val = false;
            }
            return Val;
        }
        /// <summary>
        /// 判断URL参数是否为正整数
        /// </summary>
        /// <param name="param"></param>
        public static bool isInt(this object param)
        {
            Regex regex = new Regex("(^0$)|(^-?[1-9][0-9]*$)");
            if (string.IsNullOrEmpty(param.ToString2()))
                return false;
            if (regex.IsMatch(param.ToString2()))
                return true;
            else
                return false;
        }
        /// <summary>
        /// 判断参数是否为Double(比如价格)
        /// </summary>
        /// <param name="param">参数</param>
        public static bool isDouble(this object param)
        {
            Regex regex3 = new Regex(@"(^\d*\.?\d*[0-9]+\d*$)|(^[0-9]+\d*\.\d*$)|(^0$)");
            if (string.IsNullOrEmpty(param.ToString2()))
                return false;
            if (regex3.IsMatch(param.ToString2()))
                return true;
            else
                return false;
        }
        /// <summary>
        /// 检查中英文混合字符长度(英文字符算1,中文算2)
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static int JStrLen(this string _String)
        {
            return DealString.ZhuanHuanStringASIIC(_String.ToString2()).Length;
        }
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="Str">源字符串</param>
        /// <param name="length">保留长度后面加...</param>
        /// <returns></returns>
        public static string JSubString(this object Str, int length)
        {
            return Str.JSubString(length, "...");
        }
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="Str">源字符串</param>
        /// <param name="length">保留长度</param>
        /// <param name="Sig">后面的符号 例如...</param>
        /// <returns></returns>
        public static string JSubString(this object Str, int length, string Sig)
        {
            return DealString.getSubString(Str.ToString2(), length, Sig).JHtmlEncode();
        }
        /// <summary>
        /// 去除字符串里面的HTML标签
        /// </summary>
        /// <param name="strHtml">字符串</param>
        /// <returns></returns>
        public static string JreplaceHTML(this object obj)
        {
            string strHtml = obj.ToString2();
            return DealString.replaceHTML(strHtml);
        }
        /// <summary>
        /// 去掉字符串里面的换行符
        /// </summary>
        /// <param name="Str">字符串</param>
        /// <returns></returns>
        public static string JclearString_R_N(this object Str)
        {
            return DealString.clearString_R_N(Str.ToString2());
        }
        /// <summary>
        /// 把字符串转换成16进制
        /// </summary>
        /// <param name="mStr"></param>
        /// <returns></returns>
        public static string JStrToHex(this string mStr)
        {
            return DealString.StrToHex(mStr);
 
        }
        /// <summary>
        /// 把16进制转换成字符串
        /// </summary>
        /// <param name="mHex"></param>
        /// <returns></returns>
        public static string JHexToStr(this string mHex)
        {
            return DealString.HexToStr(mHex);
        }
        /// <summary>
        /// 对字符串进行md5加密
        /// </summary>
        /// <param name="_String">字符串对象</param>
        /// <returns>加密有的字符串</returns>
        public static string Jmd5(this string _String)
        {
            return DealString.MD5(_String);
        }
        /// <summary>
        /// 取得此字符串的后缀名(针对字符串为文件名的情况,不包括.)
        /// </summary>
        /// <param name="_String">字符串对象</param>
        /// <returns>截取后的子字符串</returns>
        public static string JgetExtension(this string _String)
        {
            return DealString.getExtension(_String);
        }
        /// <summary>
        /// 格式化字符串(,1,2,3,4,5,,2,32,,)为(1,2,3,4,5,2,32), 如果为空则返回字符串0
        /// </summary>
        /// <param name="Str">原字符串</param>
        /// <param name="Sign">分割标识</param>
        /// <param name="IsMustInt">必须为Int</param>
        /// <returns></returns>
        public static string JFormatIdsValues(this string Str, string Sign, bool IsMustInt)
        {
            string[] s = Str.Split(new string[] { Sign }, StringSplitOptions.RemoveEmptyEntries);
            if (s.Length == 0)
                return "";
            ArrayList output = new ArrayList();
            Regex reg = new Regex("^-?\\d+$");
            foreach (string _s in s)
            {
                if (!string.IsNullOrEmpty(_s) && (reg.IsMatch(_s) || !IsMustInt))
                    output.Add(_s);
            }
            return output.JArrayListToString(Sign, true);
        }
        /// <summary>
        /// 将双字节字符转换成 ww
        /// </summary>
        /// <param name="_string">待转换的字符串</param>
        /// <returns>返回转换后的字符串</returns>
        public static string JDoubleByteToTwoByte(this string Str)
        {
            Regex reg = new Regex(@"[^\x00-\xff]");
            return reg.Replace(Str, "ww");
        }
        /// <summary>
        /// 将已经为HTTP传输过的HTML编码字符串转换为已解码的字符串
        /// </summary>
        /// <param name="textToFormat"></param>
        /// <returns></returns>
        public static string JHtmlDecode(this string textToFormat)
        {
            if (string.IsNullOrEmpty(textToFormat))
            {
                return textToFormat;
            }
            return HttpUtility.HtmlDecode(textToFormat);
        }
 
        /// <summary>
        /// 将字符串转换为html编码的字符串
        /// </summary>
        /// <param name="textToFormat"></param>
        /// <returns></returns>
        public static string JHtmlEncode(this object textToFormat)
        {
            string _textToFormat = textToFormat.ToString2();
            if (string.IsNullOrEmpty(_textToFormat))
            {
                return _textToFormat;
            }
            return HttpUtility.HtmlEncode(_textToFormat);
        }
 
        /// <summary>
        /// 把带有注入性的字符串转换成普通字符
        /// </summary>
        /// <param name="_string">待检查的字符串</param>
        /// <returns></returns>
        public static string FilterSQL(this string _string)
        {
            if (_string == null)
            {
                return null;
            }
            return DealString.ChangeSQL(_string);
        }
        #endregion
 
        #region ArrayList Int[] String[] String
 
        /// <summary>
        /// ArrayList转字符串,每项以,分隔
        /// </summary>
        /// <param name="_ArrayList">源ArrayList</param>
        /// <returns></returns>
        public static string JArrayListToString(this ArrayList _ArrayList)
        {
            return JArrayListToString(_ArrayList, ",", false);
        }
        /// <summary>
        /// ArrayList转字符串,每项以,分隔
        /// </summary>
        /// <param name="_ArrayList">源ArrayList</param>
        /// <param name="RemoveEmptyEntries">是否去掉 空 值</param>
        /// <returns></returns>
        public static string JArrayListToString(this ArrayList _ArrayList, bool RemoveEmptyEntries)
        {
            return JArrayListToString(_ArrayList, ",", RemoveEmptyEntries);
        }
        /// <summary>
        ///  ArrayList转字符串,每项以Sig分隔
        /// </summary>
        /// <param name="_ArrayList">源ArrayList</param>
        /// <param name="Sig">分隔符</param>
        /// <returns></returns>
        public static string JArrayListToString(this ArrayList _ArrayList, string Sig, bool RemoveEmptyEntries)
        {
            for (int i = 0; i < _ArrayList.Count; i++)
                _ArrayList[i] = _ArrayList[i].ToString2();
 
            string[] _s = (string[])_ArrayList.ToArray(typeof(string));
            if (RemoveEmptyEntries)
            {
                string Mstr = string.Join(Sig, _s);
                _s = Mstr.Split(new string[] { Sig }, StringSplitOptions.RemoveEmptyEntries);
            }
            return string.Join(Sig, _s);
        }
        /// <summary>
        /// Double?[] 转 Double[]
        /// </summary>
        /// <param name="_Arr"></param>
        /// <returns></returns>
        public static double[] JDoublesNotHasNull(this double?[] _Arr)
        {
            ArrayList List = new ArrayList();
            foreach (double? s in _Arr)
            {
                if (s != null)
                    List.Add(s);
            }
            return (double[])List.ToArray(typeof(double));
        }
        /// <summary>
        /// Int?[] 转 Int[]
        /// </summary>
        /// <param name="_Arr"></param>
        /// <returns></returns>
        public static int[] JIntsNotHasNull(this int?[] _Arr)
        {
            ArrayList List = new ArrayList();
            foreach (int? s in _Arr)
            {
                if (s != null)
                    List.Add(s);
            }
            return (int[])List.ToArray(typeof(int));
        }
        /// <summary>
        /// string[] 转 ArrayList
        /// </summary>
        /// <param name="_Arr"></param>
        /// <returns></returns>
        public static ArrayList JStringsToArrayList(this string[] _Arr)
        {
            ArrayList List = new ArrayList();
 
            if (_Arr == null)
            {
                _Arr = new string[] { };
            }
 
            foreach (string s in _Arr)
            {
                List.Add(s.ToString2());
            }
            return List;
        }
        /// <summary>
        /// int?[] 转 ArrayList
        /// </summary>
        /// <param name="_Arr"></param>
        /// <returns></returns>
        public static ArrayList JIntsToArrayList(this int?[] _Arr)
        {
            ArrayList List = new ArrayList();
            foreach (int? s in _Arr)
            {
                List.Add(s);
            }
            return List;
        }
        /// <summary>
        /// string[] 转 int?[]
        /// </summary>
        /// <param name="_Arr"></param>
        /// <returns></returns>
        public static int?[] JStringsToInts(this string[] _Arr)
        {
            ArrayList List = new ArrayList();
            foreach (string s in _Arr)
            {
                if (s.isInt())
                    List.Add(s.ToInt32());
            }
            return (int?[])List.ToArray(typeof(int?));
        }
        /// <summary>
        /// int?[] 转 string[]
        /// </summary>
        /// <param name="_Arr"></param>
        /// <param name="RemoveEmptyEntries"></param>
        /// <returns></returns>
        public static string[] JIntsToStrings(this int?[] _Arr, bool RemoveEmptyEntries)
        {
            ArrayList List = new ArrayList();
            foreach (int? s in _Arr)
            {
                if (s == null && !RemoveEmptyEntries)
                    List.Add(string.Empty);
                else
                    List.Add(s.ToString2());
            }
            return (string[])List.ToArray(typeof(string));
        }
 
        /// <summary>
        ///decimal类型转换成int强加1
        /// </summary>
        /// <param name="Num">源数据</param>
        /// <returns></returns>
        public static int JRoundInt(this decimal Num)
        {
            decimal f = Num;
            int b = 0;
            try
            {
                b = (int)f;
                if ((decimal)b < f)
                {
                    b++;
                }
            }
            catch
            {
                b = int.MaxValue;
            }
            return b;
        }
        #endregion
 
        #region 格式判断
        /// <summary>
        /// 判断参数是否为邮箱
        /// </summary>
        /// <param name="param"></param>
        public static bool isEmail(this object param)
        {
            Regex regex = new Regex("(^[_.0-9A-Za-z-]+@[0-9A-Za-z-]+.(com|cc|cn|tv|hk|name|mobi|net|biz|org|info|gov.cn|com.cn|net.cn|org.cn)$)");
            if (string.IsNullOrEmpty(param.ToString2()))
                return false;
 
            if (regex.IsMatch(param.ToString2()) && param.ToString2().Length <= 50)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 判断参数是否为QQ号码
        /// </summary>
        /// <param name="param">是true </param>
        public static bool isQQ(this object param)
        {
            Regex regex = new Regex("(^[^0]d{4,11}$)");
            if (string.IsNullOrEmpty(param.ToString2()))
                return false;
 
            if (regex.IsMatch(param.ToString2()))
                return true;
            else
                return false;
        }
 
        /// <summary>
        /// 判断参数是否为手机号码
        /// </summary>
        /// <param name="param">是true </param>
        public static bool isMobileNumber(this object param)
        {
            Regex regex = new Regex("(^[0-9]{11}$)");
            if (string.IsNullOrEmpty(param.ToString2()))
                return false;
 
            if (regex.IsMatch(param.ToString2()))
                return true;
            else
                return false;
        }
 
        #endregion
    }
}