StringEx.cs 5.4 KB

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