StringEx.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Security.Cryptography;
  7. using System.Linq.Expressions;
  8. namespace Bowin.Common.Utility
  9. {
  10. public static class StringEx
  11. {
  12. /// <summary>
  13. /// 从左边起截取指定长度的字符串
  14. /// </summary>
  15. /// <param name="input"></param>
  16. /// <param name="length"></param>
  17. /// <returns></returns>
  18. public static string Left(this string input, int length)
  19. {
  20. if (input.IsEmpty())
  21. {
  22. return string.Empty;
  23. }
  24. if (input.Length > length)
  25. {
  26. return input.Substring(0, length);
  27. }
  28. return input;
  29. }
  30. /// <summary>
  31. /// 从左边起截取指定长度的字符串,并追加指定的字符串
  32. /// </summary>
  33. /// <param name="input"></param>
  34. /// <param name="length"></param>
  35. /// <returns></returns>
  36. public static string Left(this string input, int length, string plus)
  37. {
  38. if (input.IsEmpty())
  39. {
  40. return string.Empty;
  41. }
  42. if (input.Length <= length)
  43. {
  44. return input;
  45. }
  46. return string.Format("{0}{1}", Left(input, length - 1), plus);
  47. }
  48. /// <summary>
  49. /// 验证字符串是否为null 空或者由空白字符串组成
  50. /// </summary>
  51. /// <param name="input"></param>
  52. /// <returns></returns>
  53. public static bool IsEmpty(this string input)
  54. {
  55. if (input == null || input.Trim().Length == 0)
  56. {
  57. return true;
  58. }
  59. return false;
  60. }
  61. /// <summary>
  62. /// 验证字符串不为null 空或者由空白字符串组成
  63. /// </summary>
  64. /// <param name="input"></param>
  65. /// <returns></returns>
  66. public static bool IsNotEmpty(this string input)
  67. {
  68. return !input.IsEmpty();
  69. }
  70. public static string MD5(this string input)
  71. {
  72. using (var md5 = System.Security.Cryptography.MD5.Create())
  73. {
  74. var result = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
  75. var strResult = BitConverter.ToString(result);
  76. string result3 = strResult.Replace("-", "");
  77. return result3;
  78. }
  79. }
  80. public static string ToCamel(this string input)
  81. {
  82. if (input.Length == 0)
  83. {
  84. return "";
  85. }
  86. else
  87. {
  88. string firstCode = input.Substring(0, 1).ToLower();
  89. return firstCode + input.Substring(1, input.Length - 1);
  90. }
  91. }
  92. public static string TrimStart(this string input, string partern)
  93. {
  94. if (input.StartsWith(partern))
  95. {
  96. return input.Substring(partern.Length);
  97. }
  98. else
  99. {
  100. return input;
  101. }
  102. }
  103. public static string TrimEnd(this string input, string partern)
  104. {
  105. if (input.EndsWith(partern))
  106. {
  107. return input.Remove(input.LastIndexOf(partern));
  108. }
  109. else
  110. {
  111. return input;
  112. }
  113. }
  114. public static string NoHTML(this string Htmlstring)
  115. {
  116. //删除脚本
  117. Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
  118. //删除HTML
  119. Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
  120. Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
  121. Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
  122. Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
  123. Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
  124. Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
  125. Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
  126. Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
  127. Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
  128. Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
  129. Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
  130. Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
  131. //替换单引号 避免js脚本错误 最新文章列表摘要就出错过 duanqh 2011-09-22
  132. Htmlstring = Htmlstring.Replace("'", "&acute");
  133. Htmlstring = Htmlstring.Replace("<", "&lt");
  134. Htmlstring = Htmlstring.Replace(">", "&gt");
  135. Htmlstring = Htmlstring.Replace("\r\n", "");
  136. Htmlstring = Htmlstring.Trim();
  137. return Htmlstring;
  138. }
  139. public static string RealTrim(this string source)
  140. {
  141. string result = source;
  142. result = result.Replace("\0", "");
  143. result = result.Trim();
  144. return result;
  145. }
  146. public static List<Guid?> SplitIDString(this string source, char seperator = ',')
  147. {
  148. return source.Split(seperator).Where(x => x != "").Select(x => (Guid?)new Guid(x)).ToList();
  149. }
  150. }
  151. }