简体   繁体   中英

How to check if any string from array of string contains in another string in c#

I am writing a logic something like following code -

string myString = "ABCD";

if(myString.Contains("AB") && myString.Contains("C") && !myString.Contains("XYZ"))
{
    //Do something
}

I want to write above code something like this:

string myString = "ABCD";

if( myString .contains any string from new[] { "AB", "C" } )
{
   //Do Something
}

Can you please tell me, How can I achieve it? Thanks in advance.

You want to check if the array has Any item such that myString Contains it:

using System.Linq;

...

// myString contains "AB" or "C"
if (new[]{ "AB", "C" }.Any(item => myString.Contains(item))) {
    ...
} 

Put All it myString should contains both "AB" and "C"

// myString contains both "AB" and "C"
if (new[]{ "AB", "C" }.All(item => myString.Contains(item))) {
    ...
} 

You can use .All() because you are trying to check all strings present in an array

var checkElements = new string[]{ "AB", "C"};
//All element contains in myString then this will return true. It checks all elements
if(checkElements.All(x =>  myString.Contains(x)))
{
   //Your business logic
}

If you want to check, if any string from an array checkElements , present in myString then you can use .Any() from System.Linq

var checkElements = new string[]{ "AB", "C"};
//Any one element contains in myString then this will return true.
if(checkElements.Any(x =>  myString.Contains(x)))
{
   //Your business logic
}

How about a little string extension method?

public static class StringExt
{
    public static bool ContainsAnyOf(this string self, params string[] strings)
    {
        return strings.Any(self.Contains);
    }
}

Then you can do this:

if (myString.ContainsAnyOf("AB", "C") && !myString.Contains("XYZ"))
{
    //Do something
}   

This sounds like a candidate for an extension method!

public static class StringExtensions
{
    public static bool Contains(this string self, params string[] values)
    {
        for(int ix = 0; ix < values.Length; ++ix)
            if(!self.Contains(values[ix]))
                return false;

        return true;
    }
}

For your code like:

string myString = "ABCD";
if(myString.Contains("AB") && myString.Contains("C") && !myString.Contains("XYZ"))
{
    //Do something
}

Let's do all your checks using LINQ:

var theStr = "ABCD";
var rules = new string[]{ "AB", "C", "!XYZ"};
if(rules.All(rule => rule[0] == '!' ? !theStr.Contains(rule.Substring(1)) : theStr.Contains(rule)))
{
   //Do something
}

I do feel it's perhaps a mild abuse to put the "not" into the data by prefixing it with ";" though. consider making a Rule class that has a string and an Enum of RuleType that defines whether that string should be searched as "Contains" or "Not Contains".. Especially if you are ever going to want to find strings where one of the things you're looking for really is a string that starts with an "!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM