Skip to content

Commit

Permalink
add - AllIndexesOf() overload added
Browse files Browse the repository at this point in the history
---

We've added a brand new overload that deals with the value being a character.

---

Type: add
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Sep 16, 2024
1 parent 8595ff2 commit 443cc38
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Textify.Tests/General/TextToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,20 @@ public void TestAllIndexesOf()
Indexes.ShouldBe(expected);
}

/// <summary>
/// Tests getting all indexes of a character
/// </summary>
[TestMethod]
[Description("Querying")]
public void TestAllIndexesOfChar()
{
int expected = 4;
string Source = "Nitrocid is awesome and is great!";
char Target = 'i';
int Indexes = Source.AllIndexesOf(Target).Count();
Indexes.ShouldBe(expected);
}

/// <summary>
/// Tests checking if the string contains any of the target strings.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions Textify/General/TextTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,28 @@ public static IEnumerable<int> AllIndexesOf(this string target, string value)
}
}

/// <summary>
/// Get all indexes of a value in string
/// </summary>
/// <param name="target">Source string</param>
/// <param name="value">A value</param>
/// <returns>Indexes of strings</returns>
public static IEnumerable<int> AllIndexesOf(this string target, char value)
{
if (target is null)
throw new TextifyException("The target may not be null");

int index = 0;
while (true)
{
index = target.IndexOf(value, index);
if (index == -1)
break;
yield return index;
index++;
}
}

/// <summary>
/// Formats the string
/// </summary>
Expand Down

0 comments on commit 443cc38

Please sign in to comment.