diff --git a/ICD.Common.Utils.Tests/Extensions/StringExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/StringExtensionsTest.cs index 4fff5b0..6801374 100644 --- a/ICD.Common.Utils.Tests/Extensions/StringExtensionsTest.cs +++ b/ICD.Common.Utils.Tests/Extensions/StringExtensionsTest.cs @@ -58,6 +58,13 @@ namespace ICD.Common.Utils.Tests.Extensions Assert.AreEqual(expected, value.RemoveWhitespace()); } + [TestCase("1234567890", "12345", "67890")] + [TestCase("foobarfoobar", "bar", "foofoo")] + public void RemoveStringTest(string value, string other, string expected) + { + Assert.AreEqual(expected, value.Remove(other)); + } + [TestCase("1234567890", new[] {'2', '6'}, "13457890")] [TestCase("912529434324", new[] {'-', '(', ')', '.', '+'}, "912529434324")] public void RemoveCharactersTest(string value, IEnumerable characters, string expected) diff --git a/ICD.Common.Utils/Extensions/StringExtensions.cs b/ICD.Common.Utils/Extensions/StringExtensions.cs index 6d50f3c..d1828e3 100644 --- a/ICD.Common.Utils/Extensions/StringExtensions.cs +++ b/ICD.Common.Utils/Extensions/StringExtensions.cs @@ -152,7 +152,31 @@ namespace ICD.Common.Utils.Extensions } /// - /// Removes the given characters from the string. + /// Removes all occurances of the given string. + /// + /// + /// + /// + public static string Remove(this string extends, string other) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (other == null) + throw new ArgumentNullException("other"); + + if (string.IsNullOrEmpty(other)) + return extends; + + int index; + while ((index = extends.IndexOf(other, StringComparison.Ordinal)) >= 0) + extends = extends.Remove(index, other.Length); + + return extends; + } + + /// + /// Removes all occurances the given characters from the string. /// /// ///