1
0

Added ContainsAny and ContainsAll

This commit is contained in:
2025-11-14 12:01:15 +01:00
parent c15b8b808d
commit feed3d5427
3 changed files with 92 additions and 0 deletions

View File

@@ -253,6 +253,96 @@ namespace System
public static StringBuilder AppendLine(this StringBuilder sb, string value, string 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
[GeneratedRegex(EmailRegex, RegexOptions.Compiled)]
private static partial Regex ValidEmailRegex();