Skip to content

Commit

Permalink
add - Added last index of replacement overloads
Browse files Browse the repository at this point in the history
---

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
  • Loading branch information
AptiviCEO committed Sep 16, 2024
1 parent be20d7d commit 8595ff2
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Textify.Tests/General/TextToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,46 @@ public void TestReplaceLastOccurrence()
Source.ShouldBe(expected);
}

/// <summary>
/// Tests replacing last occurrence of a string
/// </summary>
[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);
}

/// <summary>
/// Tests replacing last occurrence of a string
/// </summary>
[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);
}

/// <summary>
/// Tests replacing last occurrence of a string
/// </summary>
[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);
}

/// <summary>
/// Tests replacing all specified occurrences of strings with a single string
/// </summary>
Expand Down
59 changes: 59 additions & 0 deletions Textify/General/TextTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,65 @@ public static string ReplaceLastOccurrence(this string source, string searchText
return result;
}

/// <summary>
/// Replaces last occurrence of a text in source string with the replacement
/// </summary>
/// <param name="source">A string which has the specified text to replace</param>
/// <param name="searchText">A string to be replaced</param>
/// <param name="replace">A character to replace</param>
/// <returns>String that has its last occurrence of text replaced</returns>
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;
}

/// <summary>
/// Replaces last occurrence of a text in source string with the replacement
/// </summary>
/// <param name="source">A string which has the specified text to replace</param>
/// <param name="searchText">A character to be replaced</param>
/// <param name="replace">A string to replace</param>
/// <returns>String that has its last occurrence of text replaced</returns>
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;
}

/// <summary>
/// Replaces last occurrence of a text in source string with the replacement
/// </summary>
/// <param name="source">A string which has the specified text to replace</param>
/// <param name="searchText">A character to be replaced</param>
/// <param name="replace">A character to replace</param>
/// <returns>String that has its last occurrence of text replaced</returns>
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;
}

/// <summary>
/// Get all indexes of a value in string
/// </summary>
Expand Down

0 comments on commit 8595ff2

Please sign in to comment.