From 8595ff201446b374a818364afaf5e01d46e30877 Mon Sep 17 00:00:00 2001 From: Aptivi CEO Date: Mon, 16 Sep 2024 22:01:33 +0300 Subject: [PATCH] add - Added last index of replacement overloads --- We've added more overloads to the ReplaceLastOccurrence() function to accommodate cases where chars are involved. --- Type: add Breaking: False Doc Required: False Backport Required: False Part: 1/1 --- Textify.Tests/General/TextToolsTest.cs | 40 +++++++++++++++++ Textify/General/TextTools.cs | 59 ++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/Textify.Tests/General/TextToolsTest.cs b/Textify.Tests/General/TextToolsTest.cs index e8394e8..230eb17 100644 --- a/Textify.Tests/General/TextToolsTest.cs +++ b/Textify.Tests/General/TextToolsTest.cs @@ -80,6 +80,46 @@ public void TestReplaceLastOccurrence() Source.ShouldBe(expected); } + /// + /// Tests replacing last occurrence of a string + /// + [TestMethod] + [Description("Querying")] + public void TestReplaceLastOccurrenceChar() + { + string expected = "Nitrocid is awesome and its features are great!"; + string Source = "Nitrocid is awesome and i great!"; + Source = Source.ReplaceLastOccurrence('i', "its features are"); + Source.ShouldBe(expected); + } + + /// + /// Tests replacing last occurrence of a string + /// + [TestMethod] + [Description("Querying")] + public void TestReplaceLastOccurrenceWithChar() + { + string expected = "Nitrocid is awesome and S great!"; + string Source = "Nitrocid is awesome and is great!"; + string Target = "is"; + Source = Source.ReplaceLastOccurrence(Target, 'S'); + Source.ShouldBe(expected); + } + + /// + /// Tests replacing last occurrence of a string + /// + [TestMethod] + [Description("Querying")] + public void TestReplaceLastOccurrenceCharWithChar() + { + string expected = "Nitrocid is awesome and S great!"; + string Source = "Nitrocid is awesome and i great!"; + Source = Source.ReplaceLastOccurrence('i', 'S'); + Source.ShouldBe(expected); + } + /// /// Tests replacing all specified occurrences of strings with a single string /// diff --git a/Textify/General/TextTools.cs b/Textify/General/TextTools.cs index ba995c2..cb68035 100644 --- a/Textify/General/TextTools.cs +++ b/Textify/General/TextTools.cs @@ -541,6 +541,65 @@ public static string ReplaceLastOccurrence(this string source, string searchText return result; } + /// + /// Replaces last occurrence of a text in source string with the replacement + /// + /// A string which has the specified text to replace + /// A string to be replaced + /// A character to replace + /// String that has its last occurrence of text replaced + public static string ReplaceLastOccurrence(this string source, string searchText, char replace) + { + if (source is null) + throw new TextifyException("The source may not be null"); + if (searchText is null) + throw new TextifyException("The search text may not be null"); + + int position = source.LastIndexOf(searchText); + if (position == -1) + return source; + string result = source.Remove(position, searchText.Length).Insert(position, $"{replace}"); + return result; + } + + /// + /// Replaces last occurrence of a text in source string with the replacement + /// + /// A string which has the specified text to replace + /// A character to be replaced + /// A string to replace + /// String that has its last occurrence of text replaced + public static string ReplaceLastOccurrence(this string source, char searchText, string replace) + { + if (source is null) + throw new TextifyException("The source may not be null"); + + int position = source.LastIndexOf(searchText); + if (position == -1) + return source; + string result = source.Remove(position, 1).Insert(position, replace); + return result; + } + + /// + /// Replaces last occurrence of a text in source string with the replacement + /// + /// A string which has the specified text to replace + /// A character to be replaced + /// A character to replace + /// String that has its last occurrence of text replaced + public static string ReplaceLastOccurrence(this string source, char searchText, char replace) + { + if (source is null) + throw new TextifyException("The source may not be null"); + + int position = source.LastIndexOf(searchText); + if (position == -1) + return source; + string result = source.Remove(position, 1).Insert(position, $"{replace}"); + return result; + } + /// /// Get all indexes of a value in string ///