Added ContainsAny and ContainsAll
This commit is contained in:
@@ -4,6 +4,7 @@ image: mcr.microsoft.com/dotnet/sdk:10.0
|
|||||||
variables:
|
variables:
|
||||||
TZ: Europe/Berlin
|
TZ: Europe/Berlin
|
||||||
LANG: de
|
LANG: de
|
||||||
|
GIT_DEPTH: 0
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- build
|
- build
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- `DomainComparer` ordering alphabetically from TLD to sub-domain
|
- `DomainComparer` ordering alphabetically from TLD to sub-domain
|
||||||
- `IPAddressComparer` able to compare IPv4 and IPv6
|
- `IPAddressComparer` able to compare IPv4 and IPv6
|
||||||
- `VersionStringComparer` to compare version strings (SemVer)
|
- `VersionStringComparer` to compare version strings (SemVer)
|
||||||
|
- `ContainsAny()` and `ContainsAll()` for strings
|
||||||
- `ASPNETCORE_APPL_PROXY` environment variable can be used on proxy configuration
|
- `ASPNETCORE_APPL_PROXY` environment variable can be used on proxy configuration
|
||||||
- Support for .NET 10.0 LTS
|
- Support for .NET 10.0 LTS
|
||||||
|
|
||||||
|
|||||||
@@ -253,6 +253,96 @@ namespace System
|
|||||||
public static StringBuilder AppendLine(this StringBuilder sb, string value, string newLine)
|
public static StringBuilder AppendLine(this StringBuilder sb, string value, string newLine)
|
||||||
=> sb.Append(value).Append(newLine);
|
=> sb.Append(value).Append(newLine);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a value indicating whether any of the specified strings occurs within this string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="str">The string to search within. Cannot be null.</param>
|
||||||
|
/// <param name="values">One of the strings to seek.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// <see langword="true"/> if any of the parameters occur within this string, or if any of the <paramref name="values"/> is the empty string ("");
|
||||||
|
/// otherwise, <see langword="false"/>.
|
||||||
|
/// </returns>
|
||||||
|
public static bool ContainsAny(this string str, params string[] values)
|
||||||
|
=> ContainsAny(str, StringComparison.CurrentCulture, values);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a value indicating whether any of the specified strings occurs within this string, using the specified comparison rules.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="str">The string to search within. Cannot be null.</param>
|
||||||
|
/// <param name="comparisonType">One of the enumeration values that specifies the rules for the comparison.</param>
|
||||||
|
/// <param name="values">One of the strings to seek.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// <see langword="true"/> if any of the parameters occur within this string, or if any of the <paramref name="values"/> is the empty string ("");
|
||||||
|
/// otherwise, <see langword="false"/>.
|
||||||
|
/// </returns>
|
||||||
|
public static bool ContainsAny(this string str, StringComparison comparisonType, params string[] values)
|
||||||
|
{
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
ArgumentNullException.ThrowIfNull(str, nameof(str));
|
||||||
|
ArgumentNullException.ThrowIfNull(values, nameof(values));
|
||||||
|
#else
|
||||||
|
if (str == null)
|
||||||
|
throw new ArgumentNullException(nameof(str));
|
||||||
|
|
||||||
|
if (values == null)
|
||||||
|
throw new ArgumentNullException(nameof(values));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
string[] compareValues = values.Distinct().ToArray();
|
||||||
|
foreach (string value in compareValues)
|
||||||
|
{
|
||||||
|
if (str.Contains(value, comparisonType))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a value indicating whether all of the specified strings occurs within this string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="str">The string to search within. Cannot be null.</param>
|
||||||
|
/// <param name="values">All the strings to seek.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// <see langword="true"/> if all of the parameters occur within this string, or if all of the <paramref name="values"/> are the empty string ("");
|
||||||
|
/// otherwise, <see langword="false"/>.
|
||||||
|
/// </returns>
|
||||||
|
public static bool ContainsAll(this string str, params string[] values)
|
||||||
|
=> ContainsAll(str, StringComparison.CurrentCulture, values);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a value indicating whether all of the specified strings occurs within this string, using the specified comparison rules.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="str">The string to search within. Cannot be null.</param>
|
||||||
|
/// <param name="comparisonType">One of the enumeration values that specifies the rules for the comparison.</param>
|
||||||
|
/// <param name="values">All the strings to seek.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// <see langword="true"/> if all of the parameters occur within this string, or if all of the <paramref name="values"/> are the empty string ("");
|
||||||
|
/// otherwise, <see langword="false"/>.
|
||||||
|
/// </returns>
|
||||||
|
public static bool ContainsAll(this string str, StringComparison comparisonType, params string[] values)
|
||||||
|
{
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
ArgumentNullException.ThrowIfNull(str, nameof(str));
|
||||||
|
ArgumentNullException.ThrowIfNull(values, nameof(values));
|
||||||
|
#else
|
||||||
|
if (str == null)
|
||||||
|
throw new ArgumentNullException(nameof(str));
|
||||||
|
|
||||||
|
if (values == null)
|
||||||
|
throw new ArgumentNullException(nameof(values));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
string[] compareValues = values.Distinct().ToArray();
|
||||||
|
foreach (string value in compareValues)
|
||||||
|
{
|
||||||
|
if (!str.Contains(value, comparisonType))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
#if NET8_0_OR_GREATER
|
#if NET8_0_OR_GREATER
|
||||||
[GeneratedRegex(EmailRegex, RegexOptions.Compiled)]
|
[GeneratedRegex(EmailRegex, RegexOptions.Compiled)]
|
||||||
private static partial Regex ValidEmailRegex();
|
private static partial Regex ValidEmailRegex();
|
||||||
|
|||||||
Reference in New Issue
Block a user