BaseExtensions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Globalization;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace MediaBrowser.Common.Extensions
  7. {
  8. /// <summary>
  9. /// Class BaseExtensions
  10. /// </summary>
  11. public static class BaseExtensions
  12. {
  13. /// <summary>
  14. /// Strips the HTML.
  15. /// </summary>
  16. /// <param name="htmlString">The HTML string.</param>
  17. /// <returns>System.String.</returns>
  18. public static string StripHtml(this string htmlString)
  19. {
  20. // http://stackoverflow.com/questions/1349023/how-can-i-strip-html-from-text-in-net
  21. const string pattern = @"<(.|\n)*?>";
  22. return Regex.Replace(htmlString, pattern, string.Empty).Trim();
  23. }
  24. /// <summary>
  25. /// Replaces the specified STR.
  26. /// </summary>
  27. /// <param name="str">The STR.</param>
  28. /// <param name="oldValue">The old value.</param>
  29. /// <param name="newValue">The new value.</param>
  30. /// <param name="comparison">The comparison.</param>
  31. /// <returns>System.String.</returns>
  32. public static string Replace(this string str, string oldValue, string newValue, StringComparison comparison)
  33. {
  34. var sb = new StringBuilder();
  35. var previousIndex = 0;
  36. var index = str.IndexOf(oldValue, comparison);
  37. while (index != -1)
  38. {
  39. sb.Append(str.Substring(previousIndex, index - previousIndex));
  40. sb.Append(newValue);
  41. index += oldValue.Length;
  42. previousIndex = index;
  43. index = str.IndexOf(oldValue, index, comparison);
  44. }
  45. sb.Append(str.Substring(previousIndex));
  46. return sb.ToString();
  47. }
  48. /// <summary>
  49. /// Removes the accent.
  50. /// </summary>
  51. /// <param name="text">The text.</param>
  52. /// <returns>System.String.</returns>
  53. public static string RemoveAccent(this string text)
  54. {
  55. var normalizedString = text.Normalize(NormalizationForm.FormD);
  56. var stringBuilder = new StringBuilder();
  57. foreach (var c in normalizedString)
  58. {
  59. var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
  60. if (unicodeCategory != UnicodeCategory.NonSpacingMark)
  61. {
  62. stringBuilder.Append(c);
  63. }
  64. }
  65. return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
  66. }
  67. /// <summary>
  68. /// Gets the M d5.
  69. /// </summary>
  70. /// <param name="str">The STR.</param>
  71. /// <returns>Guid.</returns>
  72. public static Guid GetMD5(this string str)
  73. {
  74. using (var provider = MD5.Create())
  75. {
  76. return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
  77. }
  78. }
  79. /// <summary>
  80. /// Gets the MB id.
  81. /// </summary>
  82. /// <param name="str">The STR.</param>
  83. /// <param name="type">The type.</param>
  84. /// <returns>Guid.</returns>
  85. public static Guid GetMBId(this string str, Type type)
  86. {
  87. if (type == null)
  88. {
  89. throw new ArgumentNullException("type");
  90. }
  91. var key = type.FullName + str.ToLower();
  92. return key.GetMD5();
  93. }
  94. /// <summary>
  95. /// Gets the attribute value.
  96. /// </summary>
  97. /// <param name="str">The STR.</param>
  98. /// <param name="attrib">The attrib.</param>
  99. /// <returns>System.String.</returns>
  100. /// <exception cref="System.ArgumentNullException">attrib</exception>
  101. public static string GetAttributeValue(this string str, string attrib)
  102. {
  103. if (string.IsNullOrEmpty(str))
  104. {
  105. throw new ArgumentNullException("str");
  106. }
  107. if (string.IsNullOrEmpty(attrib))
  108. {
  109. throw new ArgumentNullException("attrib");
  110. }
  111. string srch = "[" + attrib + "=";
  112. int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  113. if (start > -1)
  114. {
  115. start += srch.Length;
  116. int end = str.IndexOf(']', start);
  117. return str.Substring(start, end - start);
  118. }
  119. return null;
  120. }
  121. }
  122. }