Skip to content

Commit

Permalink
add - doc - Added more string extensions
Browse files Browse the repository at this point in the history
---

We've added more string extensions:

  - StartsWithCase / StartsWithNoCase
  - EndsWithCase / EndsWithNoCase
  - ContainsWithNoCase

---

Type: add
Breaking: False
Doc Required: True
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 3, 2024
1 parent 82333ab commit 650d64c
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Textify.Tests/General/TextToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,71 @@ public void TestEqualsCase(string source, string target, bool expected)
bool result = source.EqualsCase(target);
result.ShouldBe(expected);
}

/// <summary>
/// Tests comparing strings (case-insensitive)
/// </summary>
[DataTestMethod]
[DataRow("Hello", "He", true)]
[DataRow("Hello", "HE", true)]
[Description("Querying")]
public void TestStartsWithNoCase(string source, string target, bool expected)
{
bool result = source.StartsWithNoCase(target);
result.ShouldBe(expected);
}

/// <summary>
/// Tests comparing strings (case-sensitive)
/// </summary>
[DataTestMethod]
[DataRow("Hello", "He", true)]
[DataRow("Hello", "HE", false)]
[Description("Querying")]
public void TestStartsWithCase(string source, string target, bool expected)
{
bool result = source.StartsWithCase(target);
result.ShouldBe(expected);
}

/// <summary>
/// Tests comparing strings (case-insensitive)
/// </summary>
[DataTestMethod]
[DataRow("Hello", "lo", true)]
[DataRow("Hello", "Lo", true)]
[Description("Querying")]
public void TestEndsWithNoCase(string source, string target, bool expected)
{
bool result = source.EndsWithNoCase(target);
result.ShouldBe(expected);
}

/// <summary>
/// Tests comparing strings (case-sensitive)
/// </summary>
[DataTestMethod]
[DataRow("Hello", "lo", true)]
[DataRow("Hello", "Lo", false)]
[Description("Querying")]
public void TestEndsWithCase(string source, string target, bool expected)
{
bool result = source.EndsWithCase(target);
result.ShouldBe(expected);
}

/// <summary>
/// Tests comparing strings (case-insensitive)
/// </summary>
[DataTestMethod]
[DataRow("Hello", "lo", true)]
[DataRow("Hello", "Lo", true)]
[Description("Querying")]
public void TestContainsWithNoCase(string source, string target, bool expected)
{
bool result = source.ContainsWithNoCase(target);
result.ShouldBe(expected);
}
}

}
78 changes: 78 additions & 0 deletions Textify/General/TextTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1688,5 +1688,83 @@ public static bool EqualsCase(this string source, string target, StringCompariso
throw new ArgumentException($"Comparison {comparison} not valid for case-sensitive string comparison");
return string.Equals(source, target, comparison);
}

/// <summary>
/// Checks whether the string starts with the specific target string (case-insensitive)
/// </summary>
/// <param name="source">Source string to compare</param>
/// <param name="target">Target string to compare</param>
/// <param name="comparison">Comparison type (must be case-insensitive)</param>
/// <returns>True if the source string starts with the target substring; false otherwise</returns>
/// <exception cref="ArgumentException"></exception>
public static bool StartsWithNoCase(this string source, string target, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
{
if (comparison != StringComparison.CurrentCultureIgnoreCase &&
comparison != StringComparison.InvariantCultureIgnoreCase &&
comparison != StringComparison.OrdinalIgnoreCase)
throw new ArgumentException($"Comparison {comparison} not valid for case-insensitive string comparison");
return source.StartsWith(target, comparison);
}

/// <summary>
/// Checks whether the string starts with the specific target string (case-sensitive)
/// </summary>
/// <param name="source">Source string to compare</param>
/// <param name="target">Target string to compare</param>
/// <param name="comparison">Comparison type (must be case-sensitive)</param>
/// <returns>True if the source string starts with the target substring; false otherwise</returns>
/// <exception cref="ArgumentException"></exception>
public static bool StartsWithCase(this string source, string target, StringComparison comparison = StringComparison.Ordinal)
{
if (comparison != StringComparison.CurrentCulture &&
comparison != StringComparison.InvariantCulture &&
comparison != StringComparison.Ordinal)
throw new ArgumentException($"Comparison {comparison} not valid for case-sensitive string comparison");
return source.StartsWith(target, comparison);
}

/// <summary>
/// Checks whether the string ends with the specific target string (case-insensitive)
/// </summary>
/// <param name="source">Source string to compare</param>
/// <param name="target">Target string to compare</param>
/// <param name="comparison">Comparison type (must be case-insensitive)</param>
/// <returns>True if the source string ends with the target substring; false otherwise</returns>
/// <exception cref="ArgumentException"></exception>
public static bool EndsWithNoCase(this string source, string target, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
{
if (comparison != StringComparison.CurrentCultureIgnoreCase &&
comparison != StringComparison.InvariantCultureIgnoreCase &&
comparison != StringComparison.OrdinalIgnoreCase)
throw new ArgumentException($"Comparison {comparison} not valid for case-insensitive string comparison");
return source.EndsWith(target, comparison);
}

/// <summary>
/// Checks whether the string ends with the specific target string (case-sensitive)
/// </summary>
/// <param name="source">Source string to compare</param>
/// <param name="target">Target string to compare</param>
/// <param name="comparison">Comparison type (must be case-sensitive)</param>
/// <returns>True if the source string ends with the target substring; false otherwise</returns>
/// <exception cref="ArgumentException"></exception>
public static bool EndsWithCase(this string source, string target, StringComparison comparison = StringComparison.Ordinal)
{
if (comparison != StringComparison.CurrentCulture &&
comparison != StringComparison.InvariantCulture &&
comparison != StringComparison.Ordinal)
throw new ArgumentException($"Comparison {comparison} not valid for case-sensitive string comparison");
return source.EndsWith(target, comparison);
}

/// <summary>
/// Checks whether the string contains a specific target string (case-insensitive)
/// </summary>
/// <param name="source">Source string to compare</param>
/// <param name="target">Target string to compare</param>
/// <returns>True if the source string contains a target substring; false otherwise</returns>
/// <exception cref="ArgumentException"></exception>
public static bool ContainsWithNoCase(this string source, string target) =>
source.ToLower().Contains(target.ToLower());
}
}

0 comments on commit 650d64c

Please sign in to comment.