Skip to main content
added 5 characters in body
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191

I like the ternary operator but in this case I wouldn't use it because a single expression that makes sure both variables are valid and then searching for the strings is easier to understand I think.

The v ?? "" isn't very pretty, especially the "" part which actually should be string.Empty. You could get rid of it by filtering the arrayand skiparray and skipping the empty and null elements.

public static bool ContainsAll(this string str, params string[] values)
{
    return 
        !string.IsNullOrEmpty(str) && 
        values != null &&
        values
            .Where(x => !string.IsNullOrEmpty(x))
            .All(v => str.Contains(v));
}

If you decorate the second parameter with params keyword then you can just type the values without an array (if you need to):

"text".ContainsAll("t", null);

I like the ternary operator but in this case I wouldn't use it because a single expression that makes sure both variables are valid and then searching for the strings is easier to understand I think.

The v ?? "" isn't very pretty, especially the "" part which actually should be string.Empty. You could get rid of it by filtering the arrayand skip the empty and null elements.

public static bool ContainsAll(this string str, params string[] values)
{
    return 
        !string.IsNullOrEmpty(str) && 
        values != null &&
        values
            .Where(x => !string.IsNullOrEmpty(x))
            .All(v => str.Contains(v));
}

If you decorate the second parameter with params keyword then you can just type the values without an array (if you need to):

"text".ContainsAll("t", null);

I like the ternary operator but in this case I wouldn't use it because a single expression that makes sure both variables are valid and then searching for the strings is easier to understand I think.

The v ?? "" isn't very pretty, especially the "" part which actually should be string.Empty. You could get rid of it by filtering the array and skipping the empty and null elements.

public static bool ContainsAll(this string str, params string[] values)
{
    return 
        !string.IsNullOrEmpty(str) && 
        values != null &&
        values
            .Where(x => !string.IsNullOrEmpty(x))
            .All(v => str.Contains(v));
}

If you decorate the second parameter with params keyword then you can just type the values without an array (if you need to):

"text".ContainsAll("t", null);
added 88 characters in body
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191

I wouldn't use alike the ternary operator forbut in this. If you need to check all variables for nulls then case I suggestwouldn't use it because a single expression that makes sure both variables are valid and then searching for the strings is easier to understand I think.

You could also filter the values to skip the empty and null ones to avoid theThe v ?? "" isn't very pretty, especially the "" part which actually should be string.Empty. A string always contains anYou could get rid of it by filtering the arrayand skip the empty stringand null elements.

public static bool ContainsAll(this string str, params string[] values)
{
    return 
        !string.IsNullOrEmpty(str) && 
        values != null &&
        values
            .Where(x => !string.IsNullOrEmpty(x))
            .All(v => str.Contains(v));
}

If you decorate the second parameter with paramsparams keyword then you can just type the values without an array (if you need to):

"text".ContainsAll("t", null);

I wouldn't use a ternary operator for this. If you need to check all variables for nulls then I suggest a single expression.

You could also filter the values to skip the empty and null ones to avoid the v ?? "". A string always contains an empty string.

public static bool ContainsAll(this string str, params string[] values)
{
    return 
        !string.IsNullOrEmpty(str) && 
        values != null &&
        values
            .Where(x => !string.IsNullOrEmpty(x))
            .All(v => str.Contains(v));
}

If you decorate the second parameter params then you can just type the values without an array:

"text".ContainsAll("t", null);

I like the ternary operator but in this case I wouldn't use it because a single expression that makes sure both variables are valid and then searching for the strings is easier to understand I think.

The v ?? "" isn't very pretty, especially the "" part which actually should be string.Empty. You could get rid of it by filtering the arrayand skip the empty and null elements.

public static bool ContainsAll(this string str, params string[] values)
{
    return 
        !string.IsNullOrEmpty(str) && 
        values != null &&
        values
            .Where(x => !string.IsNullOrEmpty(x))
            .All(v => str.Contains(v));
}

If you decorate the second parameter with params keyword then you can just type the values without an array (if you need to):

"text".ContainsAll("t", null);
added 3 characters in body
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191

I wouldn't use a ternary operator for this. If you need to check all variables for nulls then I suggest a single expression.

You could also filter the values to skip the empty and null ones to avoid the v ?? "". A string always contains an empty string.

public static bool ContainsAll(this string str, params string[] values)
{
    return 
        !string.IsNullOrEmpty(str) && 
        values != null &&
        values
            .Where(x => !string.IsNullOrEmpty(x))
            .All(v => str.Contains(v));
}

If you makedecorate the second parametersparameter params then you can just type the values without an array:

"text".ContainsAll("t", null);

I wouldn't use a ternary operator for this. If you need to check all variables for nulls then I suggest a single expression.

You could also filter the values to skip the empty and null ones to avoid the v ?? "". A string always contains an empty string.

public static bool ContainsAll(this string str, params string[] values)
{
    return 
        !string.IsNullOrEmpty(str) && 
        values != null &&
        values
            .Where(x => !string.IsNullOrEmpty(x))
            .All(v => str.Contains(v));
}

If you make the second parameters params then you can just type the values without an array:

"text".ContainsAll("t", null);

I wouldn't use a ternary operator for this. If you need to check all variables for nulls then I suggest a single expression.

You could also filter the values to skip the empty and null ones to avoid the v ?? "". A string always contains an empty string.

public static bool ContainsAll(this string str, params string[] values)
{
    return 
        !string.IsNullOrEmpty(str) && 
        values != null &&
        values
            .Where(x => !string.IsNullOrEmpty(x))
            .All(v => str.Contains(v));
}

If you decorate the second parameter params then you can just type the values without an array:

"text".ContainsAll("t", null);
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191
Loading