StringExtension.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. namespace Common
  9. {
  10. public static class StringExtension
  11. {
  12. /// <summary>
  13. /// 字符空判断
  14. /// </summary>
  15. /// <param name="str"></param>
  16. /// <returns></returns>
  17. public static bool IsEmpty(this string str)
  18. {
  19. if (string.IsNullOrEmpty(str))
  20. return true;
  21. return false;
  22. }
  23. /// <summary>
  24. /// 直接格式化字符串
  25. /// </summary>
  26. /// <param name="str"></param>
  27. /// <param name="args"></param>
  28. /// <returns></returns>
  29. public static string FormatMe(this string str, params object[] args)
  30. {
  31. return string.Format(str, args);
  32. }
  33. /// <summary>
  34. /// 转化为32位int
  35. /// </summary>
  36. /// <param name="str"></param>
  37. /// <returns></returns>
  38. public static int TryToInt32(this string str)
  39. {
  40. return Int32.Parse(str);
  41. }
  42. /// <summary>
  43. /// 转换时间
  44. /// </summary>
  45. /// <param name="str"></param>
  46. /// <returns></returns>
  47. public static DateTime ToDateTime(this string str)
  48. {
  49. return DateTime.Parse(str);
  50. }
  51. /// <summary>
  52. /// MD5加密
  53. /// </summary>
  54. /// <param name="myString">加密字符串</param>
  55. /// <param name="salt">随机加密码</param>
  56. /// <returns></returns>
  57. public static string GetMD5(this string str, string salt)
  58. {
  59. MD5 md5 = new MD5CryptoServiceProvider();
  60. byte[] fromData = System.Text.Encoding.Unicode.GetBytes(str + salt);
  61. byte[] targetData = md5.ComputeHash(fromData);
  62. string byte2String = null;
  63. for (int i = 0; i < targetData.Length; i++)
  64. {
  65. byte2String += targetData[i].ToString("x");
  66. }
  67. return byte2String;
  68. }
  69. /// <summary>
  70. /// 将对象转换为Int32类型,转换失败返回0
  71. /// </summary>
  72. /// <param name="str">要转换的字符串</param>
  73. /// <returns>转换后的int类型结果</returns>
  74. public static int StrToInt(this string str)
  75. {
  76. return StrToInt(str, 0);
  77. }
  78. /// <summary>
  79. /// 将对象转换为Int32类型
  80. /// </summary>
  81. /// <param name="str">要转换的字符串</param>
  82. /// <param name="defValue">缺省值</param>
  83. /// <returns>转换后的int类型结果</returns>
  84. public static int StrToInt(this string str, int defValue)
  85. {
  86. if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
  87. return defValue;
  88. int rv;
  89. if (Int32.TryParse(str, out rv))
  90. return rv;
  91. return Convert.ToInt32(StrToFloat(str, defValue));
  92. }
  93. /// <summary>
  94. /// string型转换为float型
  95. /// </summary>
  96. /// <param name="strValue">要转换的字符串</param>
  97. /// <param name="defValue">缺省值</param>
  98. /// <returns>转换后的int类型结果</returns>
  99. public static float StrToFloat(this string strValue, float defValue)
  100. {
  101. if ((strValue == null) || (strValue.Length > 10))
  102. return defValue;
  103. float intValue = defValue;
  104. if (strValue != null)
  105. {
  106. bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
  107. if (IsFloat)
  108. float.TryParse(strValue, out intValue);
  109. }
  110. return intValue;
  111. }
  112. /// <summary>
  113. /// 把特定字符删除
  114. /// </summary>
  115. /// <param name="value"></param>
  116. /// <param name="zf"></param>
  117. /// <returns></returns>
  118. public static string RemoveValue(this string value, string zf= "zx95")
  119. {
  120. return value.Replace(zf, "");
  121. }
  122. /// <summary>
  123. /// 值是否是存在枚举中
  124. /// </summary>
  125. /// <typeparam name="T"></typeparam>
  126. /// <param name="value"></param>
  127. /// <returns></returns>
  128. public static bool IsEnum<T>(this string value)
  129. where T : struct
  130. {
  131. T a;
  132. if (Enum.TryParse<T>(value, out a))
  133. {
  134. return true;
  135. }
  136. return false;
  137. }
  138. }
  139. }