2
0

StringHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. namespace MediaBrowser.Model.Extensions
  6. {
  7. /// <summary>
  8. /// Isolating these helpers allow this entire project to be easily converted to Java
  9. /// </summary>
  10. public static class StringHelper
  11. {
  12. /// <summary>
  13. /// Equalses the ignore case.
  14. /// </summary>
  15. /// <param name="str1">The STR1.</param>
  16. /// <param name="str2">The STR2.</param>
  17. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  18. public static bool EqualsIgnoreCase(string str1, string str2)
  19. {
  20. return string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase);
  21. }
  22. /// <summary>
  23. /// Indexes the of ignore case.
  24. /// </summary>
  25. /// <param name="str">The string.</param>
  26. /// <param name="value">The value.</param>
  27. /// <returns>System.Int32.</returns>
  28. public static int IndexOfIgnoreCase(string str, string value)
  29. {
  30. return str.IndexOf(value, StringComparison.OrdinalIgnoreCase);
  31. }
  32. /// <summary>
  33. /// To the string culture invariant.
  34. /// </summary>
  35. /// <param name="val">The value.</param>
  36. /// <returns>System.String.</returns>
  37. public static string ToStringCultureInvariant(int val)
  38. {
  39. return val.ToString(CultureInfo.InvariantCulture);
  40. }
  41. /// <summary>
  42. /// To the string culture invariant.
  43. /// </summary>
  44. /// <param name="val">The value.</param>
  45. /// <returns>System.String.</returns>
  46. public static string ToStringCultureInvariant(long val)
  47. {
  48. return val.ToString(CultureInfo.InvariantCulture);
  49. }
  50. /// <summary>
  51. /// To the string culture invariant.
  52. /// </summary>
  53. /// <param name="val">The value.</param>
  54. /// <returns>System.String.</returns>
  55. public static string ToStringCultureInvariant(double val)
  56. {
  57. return val.ToString(CultureInfo.InvariantCulture);
  58. }
  59. /// <summary>
  60. /// Trims the start.
  61. /// </summary>
  62. /// <param name="str">The string.</param>
  63. /// <param name="c">The c.</param>
  64. /// <returns>System.String.</returns>
  65. public static string TrimStart(string str, char c)
  66. {
  67. return str.TrimStart(c);
  68. }
  69. /// <summary>
  70. /// Splits the specified string.
  71. /// </summary>
  72. /// <param name="str">The string.</param>
  73. /// <param name="term">The term.</param>
  74. /// <returns>System.String[].</returns>
  75. public static string[] RegexSplit(string str, string term)
  76. {
  77. return Regex.Split(str, term, RegexOptions.IgnoreCase);
  78. }
  79. /// <summary>
  80. /// Splits the specified string.
  81. /// </summary>
  82. /// <param name="str">The string.</param>
  83. /// <param name="term">The term.</param>
  84. /// <param name="limit">The limit.</param>
  85. /// <returns>System.String[].</returns>
  86. public static string[] RegexSplit(string str, string term, int limit)
  87. {
  88. return new Regex(term).Split(str, limit);
  89. }
  90. /// <summary>
  91. /// Replaces the specified STR.
  92. /// </summary>
  93. /// <param name="str">The STR.</param>
  94. /// <param name="oldValue">The old value.</param>
  95. /// <param name="newValue">The new value.</param>
  96. /// <param name="comparison">The comparison.</param>
  97. /// <returns>System.String.</returns>
  98. public static string Replace(this string str, string oldValue, string newValue, StringComparison comparison)
  99. {
  100. var sb = new StringBuilder();
  101. var previousIndex = 0;
  102. var index = str.IndexOf(oldValue, comparison);
  103. while (index != -1)
  104. {
  105. sb.Append(str.Substring(previousIndex, index - previousIndex));
  106. sb.Append(newValue);
  107. index += oldValue.Length;
  108. previousIndex = index;
  109. index = str.IndexOf(oldValue, index, comparison);
  110. }
  111. sb.Append(str.Substring(previousIndex));
  112. return sb.ToString();
  113. }
  114. public static string FirstToUpper(this string str)
  115. {
  116. return string.IsNullOrEmpty(str) ? "" : str.Substring(0, 1).ToUpper() + str.Substring(1);
  117. }
  118. }
  119. }