Skip to content

Commit

Permalink
add - Added replacement of chars/strings in bi-di ...
Browse files Browse the repository at this point in the history
...way

---

We've added two replacement function's overloads that defines the arguments interchangably between strings and characters:

  - ReplaceAll(this string target, string[] toBeReplaced, string toReplace)
  - ReplaceAll(this string target, string[] toBeReplaced, char toReplace)
  - ReplaceAll(this string target, char[] toBeReplaced, string toReplace)
  - ReplaceAll(this string target, char[] toBeReplaced, char toReplace)
  - ReplaceAllRange(this string target, string[] toBeReplaced, string[] toReplace)
  - ReplaceAllRange(this string target, string[] toBeReplaced, char[] toReplace)
  - ReplaceAllRange(this string target, char[] toBeReplaced, string[] toReplace)
  - ReplaceAllRange(this string target, char[] toBeReplaced, char[] toReplace)

---

Type: add
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Sep 16, 2024
1 parent 8c6bedd commit be20d7d
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 0 deletions.
78 changes: 78 additions & 0 deletions Textify.Tests/General/TextToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,45 @@ public void TestReplaceAll()
TargetString.ShouldBe(ExpectedString);
}

/// <summary>
/// Tests replacing all specified occurrences of strings with a single character
/// </summary>
[TestMethod]
[Description("Querying")]
public void TestReplaceAllWithChar()
{
string ExpectedString = "Please c Nitrocid. This sub is a unit c.";
string TargetString = "Please <replace> Nitrocid. This sub is a unit <replace2>.";
TargetString = TargetString.ReplaceAll(["<replace>", "<replace2>"], 'c');
TargetString.ShouldBe(ExpectedString);
}

/// <summary>
/// Tests replacing all specified occurrences of characters with a single string
/// </summary>
[TestMethod]
[Description("Querying")]
public void TestReplaceAllChars()
{
string ExpectedString = "PleaEsse NEsstrocEssd. ThEssEss Essub EssEss a unEsst z.";
string TargetString = "Please Nitrocid. This sub is a unit z.";
TargetString = TargetString.ReplaceAll(['s', 'i'], "Ess");
TargetString.ShouldBe(ExpectedString);
}

/// <summary>
/// Tests replacing all specified occurrences of characters with a single character
/// </summary>
[TestMethod]
[Description("Querying")]
public void TestReplaceAllCharsWithChar()
{
string ExpectedString = "Pleaze Nztroczd. Thzz zub zz a unzt z.";
string TargetString = "Please Nitrocid. This sub is a unit z.";
TargetString = TargetString.ReplaceAll(['i', 's'], 'z');
TargetString.ShouldBe(ExpectedString);
}

/// <summary>
/// Tests replacing all specified occurrences of strings with multiple strings
/// </summary>
Expand All @@ -106,6 +145,45 @@ public void TestReplaceAllRange()
TargetString.ShouldBe(ExpectedString);
}

/// <summary>
/// Tests replacing all specified occurrences of strings with multiple characters
/// </summary>
[TestMethod]
[Description("Querying")]
public void TestReplaceAllRangeWithChars()
{
string ExpectedString = "Please Z Nitrocid. This sub is a unit T.";
string TargetString = "Please <replace> Nitrocid. This sub is a unit <replace2>.";
TargetString = TargetString.ReplaceAllRange(["<replace>", "<replace2>"], ['Z', 'T']);
TargetString.ShouldBe(ExpectedString);
}

/// <summary>
/// Tests replacing all specified occurrences of characters with multiple strings
/// </summary>
[TestMethod]
[Description("Querying")]
public void TestReplaceAllRangeChars()
{
string ExpectedString = "Please test the integrity of Nitrocid. This sub is a unit test.";
string TargetString = "Please < Nitrocid. This sub is a unit >.";
TargetString = TargetString.ReplaceAllRange(['<', '>'], ["test the integrity of", "test"]);
TargetString.ShouldBe(ExpectedString);
}

/// <summary>
/// Tests replacing all specified occurrences of characters with multiple characters
/// </summary>
[TestMethod]
[Description("Querying")]
public void TestReplaceAllRangeCharsWithChars()
{
string ExpectedString = "Please Z Nitrocid. This sub is a unit T.";
string TargetString = "Please > Nitrocid. This sub is a unit <.";
TargetString = TargetString.ReplaceAllRange(['>', '<'], ['Z', 'T']);
TargetString.ShouldBe(ExpectedString);
}

/// <summary>
/// Tests string formatting
/// </summary>
Expand Down
135 changes: 135 additions & 0 deletions Textify/General/TextTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,66 @@ public static string ReplaceAll(this string target, string[] toBeReplaced, strin
return target;
}

/// <summary>
/// Replaces all the instances of strings with a string
/// </summary>
/// <param name="target">Target string</param>
/// <param name="toBeReplaced">Strings to be replaced</param>
/// <param name="toReplace">Character to replace with</param>
/// <returns>Modified string</returns>
/// <exception cref="ArgumentNullException"></exception>
public static string ReplaceAll(this string target, string[] toBeReplaced, char toReplace)
{
if (target is null)
throw new TextifyException("The target may not be null");
if (toBeReplaced is null || toBeReplaced.Length == 0)
throw new TextifyException("Array of to be replaced strings may not be null");

foreach (string ReplaceTarget in toBeReplaced)
target = target.Replace(ReplaceTarget, $"{toReplace}");
return target;
}

/// <summary>
/// Replaces all the instances of strings with a string
/// </summary>
/// <param name="target">Target string</param>
/// <param name="toBeReplaced">Characters to be replaced</param>
/// <param name="toReplace">String to replace with</param>
/// <returns>Modified string</returns>
/// <exception cref="ArgumentNullException"></exception>
public static string ReplaceAll(this string target, char[] toBeReplaced, string toReplace)
{
if (target is null)
throw new TextifyException("The target may not be null");
if (toBeReplaced is null || toBeReplaced.Length == 0)
throw new TextifyException("Array of to be replaced strings may not be null");

foreach (char ReplaceTarget in toBeReplaced)
target = target.Replace($"{ReplaceTarget}", toReplace);
return target;
}

/// <summary>
/// Replaces all the instances of strings with a string
/// </summary>
/// <param name="target">Target string</param>
/// <param name="toBeReplaced">Characters to be replaced</param>
/// <param name="toReplace">Character to replace with</param>
/// <returns>Modified string</returns>
/// <exception cref="ArgumentNullException"></exception>
public static string ReplaceAll(this string target, char[] toBeReplaced, char toReplace)
{
if (target is null)
throw new TextifyException("The target may not be null");
if (toBeReplaced is null || toBeReplaced.Length == 0)
throw new TextifyException("Array of to be replaced strings may not be null");

foreach (char ReplaceTarget in toBeReplaced)
target = target.Replace(ReplaceTarget, toReplace);
return target;
}

/// <summary>
/// Replaces all the instances of strings with a string assigned to each entry
/// </summary>
Expand All @@ -385,6 +445,81 @@ public static string ReplaceAllRange(this string target, string[] toBeReplaced,
return target;
}

/// <summary>
/// Replaces all the instances of strings with a character assigned to each entry
/// </summary>
/// <param name="target">Target string</param>
/// <param name="toBeReplaced">Strings to be replaced</param>
/// <param name="toReplace">Characters to replace with</param>
/// <returns>Modified string</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static string ReplaceAllRange(this string target, string[] toBeReplaced, char[] toReplace)
{
if (target is null)
throw new TextifyException("The target may not be null");
if (toBeReplaced is null || toBeReplaced.Length == 0)
throw new TextifyException("Array of to be replaced strings may not be null");
if (toReplace is null || toReplace.Length == 0)
throw new TextifyException("Array of to be replacement characters may not be null");
if (toBeReplaced.Length != toReplace.Length)
throw new TextifyException("Array length of which strings to be replaced doesn't equal the array length of which characters to replace.");

for (int i = 0, loopTo = toBeReplaced.Length - 1; i <= loopTo; i++)
target = target.Replace(toBeReplaced[i], $"{toReplace[i]}");
return target;
}

/// <summary>
/// Replaces all the instances of characters with a string assigned to each entry
/// </summary>
/// <param name="target">Target string</param>
/// <param name="toBeReplaced">Characters to be replaced</param>
/// <param name="toReplace">Strings to replace with</param>
/// <returns>Modified string</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static string ReplaceAllRange(this string target, char[] toBeReplaced, string[] toReplace)
{
if (target is null)
throw new TextifyException("The target may not be null");
if (toBeReplaced is null || toBeReplaced.Length == 0)
throw new TextifyException("Array of to be replaced characters may not be null");
if (toReplace is null || toReplace.Length == 0)
throw new TextifyException("Array of to be replacement strings may not be null");
if (toBeReplaced.Length != toReplace.Length)
throw new TextifyException("Array length of which characters to be replaced doesn't equal the array length of which strings to replace.");

for (int i = 0, loopTo = toBeReplaced.Length - 1; i <= loopTo; i++)
target = target.Replace($"{toBeReplaced[i]}", toReplace[i]);
return target;
}

/// <summary>
/// Replaces all the instances of characters with a character assigned to each entry
/// </summary>
/// <param name="target">Target string</param>
/// <param name="toBeReplaced">Characters to be replaced</param>
/// <param name="toReplace">Characters to replace with</param>
/// <returns>Modified string</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static string ReplaceAllRange(this string target, char[] toBeReplaced, char[] toReplace)
{
if (target is null)
throw new TextifyException("The target may not be null");
if (toBeReplaced is null || toBeReplaced.Length == 0)
throw new TextifyException("Array of to be replaced characters may not be null");
if (toReplace is null || toReplace.Length == 0)
throw new TextifyException("Array of to be replacement characters may not be null");
if (toBeReplaced.Length != toReplace.Length)
throw new TextifyException("Array length of which characters to be replaced doesn't equal the array length of which strings to replace.");

for (int i = 0, loopTo = toBeReplaced.Length - 1; i <= loopTo; i++)
target = target.Replace(toBeReplaced[i], toReplace[i]);
return target;
}

/// <summary>
/// Replaces last occurrence of a text in source string with the replacement
/// </summary>
Expand Down

0 comments on commit be20d7d

Please sign in to comment.