Util.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace VaporRecoveryOnlineWatchHubApp
  7. {
  8. public static class Util
  9. {
  10. // 中式四舍五入
  11. public static decimal Round<T>(T input, int digits = 2)
  12. {
  13. return Math.Round(Convert.ToDecimal(input), digits, MidpointRounding.AwayFromZero);
  14. }
  15. // 中式四舍五入
  16. public static double RoundToDouble<T>(T input, int digits = 2)
  17. {
  18. return (double)Math.Round(Convert.ToDecimal(input), digits, MidpointRounding.AwayFromZero);
  19. }
  20. }
  21. public static class StringExtension
  22. {
  23. //"""全角转半角"""
  24. /// 转全角的函数(SBC case)
  25. ///
  26. ///任意字符串
  27. ///全角字符串
  28. ///
  29. ///全角空格为12288,半角空格为32
  30. ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  31. ///
  32. public static string ToSBC(this string input)
  33. {
  34. // 半角转全角:
  35. char[] c = input.ToCharArray();
  36. for (int i = 0; i < c.Length; i++)
  37. {
  38. if (c[i] == 32)
  39. {
  40. c[i] = (char)12288;
  41. continue;
  42. }
  43. if (c[i] < 127)
  44. c[i] = (char)(c[i] + 65248);
  45. }
  46. return new string(c);
  47. }
  48. /**/
  49. // /
  50. // / 转半角的函数(DBC case)
  51. // /
  52. // /任意字符串
  53. // /半角字符串
  54. // /
  55. // /全角空格为12288,半角空格为32
  56. // /其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
  57. // /
  58. public static string ToDBC(this string input)
  59. {
  60. char[] c = input.ToCharArray();
  61. for (int i = 0; i < c.Length; i++)
  62. {
  63. if (c[i] == 12288)
  64. {
  65. c[i] = (char)32;
  66. continue;
  67. }
  68. if (c[i] > 65280 && c[i] < 65375)
  69. c[i] = (char)(c[i] - 65248);
  70. }
  71. return new string(c);
  72. }
  73. // 移除空格
  74. public static string RemoveSpaces(this string input)
  75. {
  76. return input.Replace(" ", "");
  77. }
  78. // 移除String List里的元素
  79. public static string RemoveStringList(this string input, List<string> strList)
  80. {
  81. var output = input;
  82. strList.ForEach(s => {
  83. output = output.Replace(s, "");
  84. });
  85. return output;
  86. }
  87. // 移除前缀字符串
  88. public static string RemovePrefixString(this string self, string str)
  89. {
  90. string strRegex = @"^(" + str + ")";
  91. return Regex.Replace(self, strRegex, "");
  92. }
  93. // 移除后缀字符串
  94. public static string RemoveSuffixString(this string self, string str)
  95. {
  96. string strRegex = @"(" + str + ")" + "$";
  97. return Regex.Replace(self, strRegex, "");
  98. }
  99. // 是否为Email
  100. public static bool IsEmail(this string self)
  101. {
  102. return self.RegexMatch(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
  103. }
  104. // 是否为域名
  105. public static bool IsDomain(this string self)
  106. {
  107. return self.RegexMatch(@"[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?");
  108. }
  109. // 是否为IP地址
  110. public static bool IsIP(this string self)
  111. {
  112. return self.RegexMatch(@"((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))");
  113. }
  114. // 是否为手机号码
  115. public static bool IsMobilePhone(this string self)
  116. {
  117. return self.RegexMatch(@"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");
  118. }
  119. // 匹配正则表达式
  120. public static bool RegexMatch(this string slef, string pattern)
  121. {
  122. Regex reg = new Regex(pattern);
  123. return reg.Match(slef).Success;
  124. }
  125. // 转换为MD5, 加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
  126. public static string ConvertToMD5(this string self, string flag = "x2")
  127. {
  128. byte[] sor = Encoding.UTF8.GetBytes(self);
  129. MD5 md5 = MD5.Create();
  130. byte[] result = md5.ComputeHash(sor);
  131. StringBuilder strbul = new StringBuilder(40);
  132. for (int i = 0; i < result.Length; i++)
  133. {
  134. strbul.Append(result[i].ToString(flag));
  135. }
  136. return strbul.ToString();
  137. }
  138. // 转换为32位MD5
  139. public static string ConvertToMD5_32(this string self)
  140. {
  141. return ConvertToMD5(self, "x2");
  142. }
  143. // 转换为48位MD5
  144. public static string ConvertToMD5_48(this string self)
  145. {
  146. return ConvertToMD5(self, "x3");
  147. }
  148. // 转换为64位MD5
  149. public static string ConvertToMD5_64(this string self)
  150. {
  151. return ConvertToMD5(self, "x4");
  152. }
  153. }
  154. }