mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
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:
@@ -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)
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user