StringEx.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. namespace ZcPeng.PublicLibrary
  5. {
  6. /// <summary>
  7. /// StringEx
  8. /// 功能:string辅助类。
  9. /// 作者:彭昭成
  10. /// 时间:2018年12月10日
  11. /// </summary>
  12. public static class StringEx
  13. {
  14. /// <summary>
  15. /// 作用:去掉字符串中最后包含的子字符串
  16. /// 如果字符串source的最后是子字符串value,则去掉最后的子字符串value;否则原样返回source。
  17. /// </summary>
  18. /// <param name="source">源字符串</param>
  19. /// <param name="value">需要被去掉的子字符串</param>
  20. /// <returns>返回的结果字符串</returns>
  21. public static string RemoveEndsWith(string source, string value)
  22. {
  23. string result;
  24. if (source.EndsWith(value))
  25. {
  26. int pos = source.LastIndexOf(value);
  27. result = source.Substring(0, pos);
  28. }
  29. else
  30. result = source;
  31. return result;
  32. }
  33. /// <summary>
  34. /// 作用:去掉字符串中开始包含的子字符串
  35. /// 如果字符串source的开始是子字符串value,则去掉开始的子字符串value;否则原样返回source。
  36. /// </summary>
  37. /// <param name="source">源字符串</param>
  38. /// <param name="value">需要被去掉的子字符串</param>
  39. /// <returns>返回的结果字符串</returns>
  40. public static string RemoveStartsWith(string source, string value)
  41. {
  42. string result;
  43. if (source.StartsWith(value))
  44. {
  45. int length = source.Length - value.Length;
  46. result = source.Substring(value.Length, length);
  47. }
  48. else
  49. result = source;
  50. return result;
  51. }
  52. /// <summary>
  53. /// 截取字符串
  54. /// </summary>
  55. /// <param name="source">源字符串</param>
  56. /// <param name="length">截取长度</param>
  57. /// <returns>返回截取之后的字符串</returns>
  58. public static string Cut(string source, int length)
  59. {
  60. string result = source;
  61. if (result.Length > length)
  62. {
  63. if (length > 3)
  64. result = result.Substring(0, length - 3) + "...";
  65. else
  66. result = result.Substring(0, length);
  67. }
  68. return result;
  69. }
  70. /// <summary>
  71. /// 作用:重复指定的字符串若干次,并返回结果。例如 RepeatString("a",3) 返回"aaa"
  72. /// </summary>
  73. /// <param name="source">需要被重复的字符串</param>
  74. /// <param name="repeatCount">重复次数</param>
  75. /// <returns>返回结果字符串</returns>
  76. public static string RepeatString(string source, int repeatCount)
  77. {
  78. StringBuilder sbResult = new StringBuilder();
  79. for (int i = 0; i < repeatCount; i++)
  80. {
  81. sbResult.Append(source);
  82. }
  83. return sbResult.ToString();
  84. }
  85. /// <summary>
  86. /// 清除HTML标签的多余
  87. /// </summary>
  88. /// <param name="el">标签名</param>
  89. /// <param name="str">源字符串</param>
  90. /// <returns></returns>
  91. private static string repElement(string el, string str)
  92. {
  93. string pat = @"<" + el + "[^>]+>";
  94. string rep = "";
  95. str = Regex.Replace(str.ToString(), pat, rep);
  96. return str;
  97. }
  98. /// <summary>
  99. /// 里面全是要清理的html标签
  100. /// </summary>
  101. /// <param name="str"></param>
  102. /// <returns></returns>
  103. public static string rep(string str)
  104. {
  105. string[] el = new string[] { "p", "span", "strong", "FONT", "P", "SPAN", "STRONG" };
  106. foreach (string s in el)
  107. {
  108. str = repElement(s, str);
  109. }
  110. return str;
  111. }
  112. /// <summary>
  113. /// 返回一个数组长度为3的字符串数组
  114. /// </summary>
  115. /// <returns>GetWeekDay[0]=周次;GetWeekDay[1]=该周第一天;GetWeekDay[2]=该周最后一天</returns>
  116. public static string[] GetWeekDay(DateTime rq)
  117. {
  118. string[] inti = new string[3];
  119. DateTime day = DateTime.Parse(rq.Year + "-1-1");
  120. System.DayOfWeek dateTime = day.DayOfWeek;
  121. int DayCount = DateTime.Today.DayOfYear;
  122. int i = (DayCount + Convert.ToInt32(dateTime) - 2) / 7 + 1;
  123. inti[0] = i.ToString();
  124. inti[1] = day.AddDays(DayCount - 1).ToString("yyyy-MM-dd");
  125. inti[2] = day.AddDays(DayCount + 5).ToString("yyyy-MM-dd");
  126. return inti;
  127. }
  128. /// <summary>
  129. /// 去除HTML代码
  130. /// </summary>
  131. /// <param name="Str"></param>
  132. /// <returns></returns>
  133. public static string LostHTML(string Str)
  134. {
  135. string Re_STR = "";
  136. if (Str != null)
  137. {
  138. if (Str != string.Empty)
  139. {
  140. string pattern = "<\v*[^<>]*>";
  141. Re_STR = Regex.Replace(Str, pattern, "");
  142. }
  143. }
  144. return (Re_STR.Replace("\\r\\n", "")).Replace("\\r", "");
  145. }
  146. }
  147. }