How to check if a string contains a list value and separately if it contains, but with a different value

I am trying to figure out how to determine if a string / does not contain a list value, but also contains, but with a different value.

If I have an input line:

string inputString = "it was one"; 

and I want to find a specific value for the condition:

 var numbList = new List<string> {"zero", "one", "two"}; if (!numbList.Any(inputString.Contains)) { Console.WriteLine("string does not contains list value"); } else { Console.WriteLine("string contains list value"); } 

But I'm not sure what is right if I want to know also about the third condition, if the string contains a value, but also contains other words.

For string: inputString = "it was one"; The desired result should be:

  Console.WriteLine("string contains list value and other words"); 

for string: inputString = "one";

  Console.WriteLine("string contains list value"); 

and for: inputString = "it was";

  Console.WriteLine( "string does not contains list value"); 
+5
source share
2 answers

I think you are looking for something like this:

 if (inputString.Split(new string[]{" "},StringSplitOptions.RemoveEmptyEntries).All(x => numbList.Contains(x))) { opDisplay="string contains list value"; } else if (numbList.Any(x => inputString.Contains(x))) { opDisplay = "string contains list value and other words"; } else { opDisplay = "string does not contains list value"; } 

You can try an example here

+4
source

why not use the code below? I cannot come up with one condition in which he fails.

  string inputString = "it was one "; var numbList = new List<string> { "zero", "one", "two" }; if (numbList.Any(x => inputString.Contains(x))) { if (numbList.Any(x => inputString.Trim().StartsWith(x) && inputString.Trim().EndsWith(x))) { Console.WriteLine("string contains list value"); } else { Console.WriteLine("string contains list value and other words"); } } else { Console.WriteLine("string does not contains list value"); } 

find the script here

+1
source

Source: https://habr.com/ru/post/1262161/


All Articles