StringExtensions.cs 804 B

123456789101112131415161718192021222324252627282930
  1. using System;
  2. using System.Text;
  3. namespace Emby.Naming
  4. {
  5. internal static class StringExtensions
  6. {
  7. public static string Replace(this string str, string oldValue, string newValue, StringComparison comparison)
  8. {
  9. var sb = new StringBuilder();
  10. var previousIndex = 0;
  11. var index = str.IndexOf(oldValue, comparison);
  12. while (index != -1)
  13. {
  14. sb.Append(str.Substring(previousIndex, index - previousIndex));
  15. sb.Append(newValue);
  16. index += oldValue.Length;
  17. previousIndex = index;
  18. index = str.IndexOf(oldValue, index, comparison);
  19. }
  20. sb.Append(str.Substring(previousIndex));
  21. return sb.ToString();
  22. }
  23. }
  24. }