feat: Adding string extension method for removing all instances of a given string, fixes unexpected use of Remove characters method

This commit is contained in:
Chris Cameron
2018-11-12 10:37:16 -05:00
parent 6b37db3530
commit 42cc8c2cfc
2 changed files with 32 additions and 1 deletions

View File

@@ -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<char> characters, string expected)

View File

@@ -152,7 +152,31 @@ namespace ICD.Common.Utils.Extensions
}
/// <summary>
/// Removes the given characters from the string.
/// Removes all occurances of the given string.
/// </summary>
/// <param name="extends"></param>
/// <param name="other"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Removes all occurances the given characters from the string.
/// </summary>
/// <param name="extends"></param>
/// <param name="characters"></param>