0

I have below code. But I am looking for opposite of Contains. Meaning, it it does not contain, I want to perform an action. Any suggestion?

string stringToCheck = "text1";
string[] stringArray = { "text1", "testtest", "test1test2", "test2text1" };
foreach (string x in stringArray)
{
    if (stringToCheck.Contains(x))
    {
        // Process...
    }
}
KotiK
  • 31
  • 6
  • 6
    `if (!stringToCheck.Contains(x))` string.Contains gives a boolean result (true/false) and `!` negates a boolean value i.e. true -> false and false -> true – Ross Gurbutt May 12 '20 at 23:28
  • 3
    Duplicate of [C# string does not contain possible?](https://stackoverflow.com/questions/6177352/c-sharp-string-does-not-contain-possible) or [Can I 'invert' a bool?](https://stackoverflow.com/q/8912353/8967612) – 41686d6564 stands w. Palestine May 12 '20 at 23:32
  • 1
    Also consider making `stringArray` a `HashSet` if it is going to have many entries. – mjwills May 12 '20 at 23:39

1 Answers1

1
string stringToCheck = "text1";
string[] stringArray = { "text1", "testtest", "test1test2", "test2text1" };
foreach (string x in stringArray)
{
    if (!stringToCheck.Contains(x))
    {
        // Process...
    }
}

or

string stringToCheck = "text1";
string[] stringArray = { "text1", "testtest", "test1test2", "test2text1" };
foreach (string x in stringArray)
{
    if (stringToCheck.Contains(x) == false)
    {
        // Process...
    }
}

The contains returns a bool value. True or false control can be done.

R.S
  • 66
  • 5