From be20d7d6b6b829d3ec6853a31b7dfc589089d0c2 Mon Sep 17 00:00:00 2001 From: Aptivi CEO Date: Mon, 16 Sep 2024 21:56:51 +0300 Subject: [PATCH] add - Added replacement of chars/strings in bi-di ... ...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 --- Textify.Tests/General/TextToolsTest.cs | 78 ++++++++++++++ Textify/General/TextTools.cs | 135 +++++++++++++++++++++++++ 2 files changed, 213 insertions(+) diff --git a/Textify.Tests/General/TextToolsTest.cs b/Textify.Tests/General/TextToolsTest.cs index 8fc9e70..e8394e8 100644 --- a/Textify.Tests/General/TextToolsTest.cs +++ b/Textify.Tests/General/TextToolsTest.cs @@ -93,6 +93,45 @@ public void TestReplaceAll() TargetString.ShouldBe(ExpectedString); } + /// + /// Tests replacing all specified occurrences of strings with a single character + /// + [TestMethod] + [Description("Querying")] + public void TestReplaceAllWithChar() + { + string ExpectedString = "Please c Nitrocid. This sub is a unit c."; + string TargetString = "Please Nitrocid. This sub is a unit ."; + TargetString = TargetString.ReplaceAll(["", ""], 'c'); + TargetString.ShouldBe(ExpectedString); + } + + /// + /// Tests replacing all specified occurrences of characters with a single string + /// + [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); + } + + /// + /// Tests replacing all specified occurrences of characters with a single character + /// + [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); + } + /// /// Tests replacing all specified occurrences of strings with multiple strings /// @@ -106,6 +145,45 @@ public void TestReplaceAllRange() TargetString.ShouldBe(ExpectedString); } + /// + /// Tests replacing all specified occurrences of strings with multiple characters + /// + [TestMethod] + [Description("Querying")] + public void TestReplaceAllRangeWithChars() + { + 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); + } + + /// + /// Tests replacing all specified occurrences of characters with multiple strings + /// + [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); + } + + /// + /// Tests replacing all specified occurrences of characters with multiple characters + /// + [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); + } + /// /// Tests string formatting /// diff --git a/Textify/General/TextTools.cs b/Textify/General/TextTools.cs index 34e6b84..ba995c2 100644 --- a/Textify/General/TextTools.cs +++ b/Textify/General/TextTools.cs @@ -360,6 +360,66 @@ public static string ReplaceAll(this string target, string[] toBeReplaced, strin return target; } + /// + /// Replaces all the instances of strings with a string + /// + /// Target string + /// Strings to be replaced + /// Character to replace with + /// Modified string + /// + 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; + } + + /// + /// Replaces all the instances of strings with a string + /// + /// Target string + /// Characters to be replaced + /// String to replace with + /// Modified string + /// + 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; + } + + /// + /// Replaces all the instances of strings with a string + /// + /// Target string + /// Characters to be replaced + /// Character to replace with + /// Modified string + /// + 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; + } + /// /// Replaces all the instances of strings with a string assigned to each entry /// @@ -385,6 +445,81 @@ public static string ReplaceAllRange(this string target, string[] toBeReplaced, return target; } + /// + /// Replaces all the instances of strings with a character assigned to each entry + /// + /// Target string + /// Strings to be replaced + /// Characters to replace with + /// Modified string + /// + /// + 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; + } + + /// + /// Replaces all the instances of characters with a string assigned to each entry + /// + /// Target string + /// Characters to be replaced + /// Strings to replace with + /// Modified string + /// + /// + 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; + } + + /// + /// Replaces all the instances of characters with a character assigned to each entry + /// + /// Target string + /// Characters to be replaced + /// Characters to replace with + /// Modified string + /// + /// + 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; + } + /// /// Replaces last occurrence of a text in source string with the replacement ///