StringHelpers.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. namespace Masuit.LuceneEFCore.SearchEngine.Extensions
  6. {
  7. public static class StringHelpers
  8. {
  9. /// <summary>
  10. /// 移除字符串的指定字符
  11. /// </summary>
  12. /// <param name="s"></param>
  13. /// <param name="chars"></param>
  14. /// <returns></returns>
  15. internal static string RemoveCharacters(this string s, IEnumerable<char> chars)
  16. {
  17. return string.IsNullOrEmpty(s) ? string.Empty : new string(s.Where(c => !chars.Contains(c)).ToArray());
  18. }
  19. /// <summary>
  20. /// 去除html标签后并截取字符串
  21. /// </summary>
  22. /// <param name="html">源html</param>
  23. /// <returns></returns>
  24. internal static string RemoveHtmlTag(this string html)
  25. {
  26. var strText = Regex.Replace(html, "<[^>]+>", "");
  27. strText = Regex.Replace(strText, "&[^;]+;", "");
  28. return strText;
  29. }
  30. /// <summary>
  31. /// 添加多个元素
  32. /// </summary>
  33. /// <typeparam name="T"></typeparam>
  34. /// <param name="this"></param>
  35. /// <param name="values"></param>
  36. public static void AddRange<T>(this ICollection<T> @this, IEnumerable<T> values)
  37. {
  38. foreach (var obj in values)
  39. {
  40. @this.Add(obj);
  41. }
  42. }
  43. /// <summary>
  44. /// 移除符合条件的元素
  45. /// </summary>
  46. /// <typeparam name="T"></typeparam>
  47. /// <param name="this"></param>
  48. /// <param name="where"></param>
  49. public static void RemoveWhere<T>(this ICollection<T> @this, Func<T, bool> @where)
  50. {
  51. foreach (var obj in @this.Where(where).ToList())
  52. {
  53. @this.Remove(obj);
  54. }
  55. }
  56. }
  57. }