StringHelper.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Text;
  3. namespace MediaBrowser.Model.Extensions
  4. {
  5. /// <summary>
  6. /// Isolating these helpers allow this entire project to be easily converted to Java
  7. /// </summary>
  8. public static class StringHelper
  9. {
  10. /// <summary>
  11. /// Equalses the ignore case.
  12. /// </summary>
  13. /// <param name="str1">The STR1.</param>
  14. /// <param name="str2">The STR2.</param>
  15. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  16. public static bool EqualsIgnoreCase(string str1, string str2)
  17. {
  18. return string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase);
  19. }
  20. /// <summary>
  21. /// Replaces the specified STR.
  22. /// </summary>
  23. /// <param name="str">The STR.</param>
  24. /// <param name="oldValue">The old value.</param>
  25. /// <param name="newValue">The new value.</param>
  26. /// <param name="comparison">The comparison.</param>
  27. /// <returns>System.String.</returns>
  28. public static string Replace(this string str, string oldValue, string newValue, StringComparison comparison)
  29. {
  30. var sb = new StringBuilder();
  31. var previousIndex = 0;
  32. var index = str.IndexOf(oldValue, comparison);
  33. while (index != -1)
  34. {
  35. sb.Append(str.Substring(previousIndex, index - previousIndex));
  36. sb.Append(newValue);
  37. index += oldValue.Length;
  38. previousIndex = index;
  39. index = str.IndexOf(oldValue, index, comparison);
  40. }
  41. sb.Append(str.Substring(previousIndex));
  42. return sb.ToString();
  43. }
  44. public static string FirstToUpper(this string str)
  45. {
  46. return string.IsNullOrEmpty(str) ? string.Empty : str.Substring(0, 1).ToUpperInvariant() + str.Substring(1);
  47. }
  48. }
  49. }