376

I want to use C# to check if a string value contains a word in a string array. For example,

string stringToCheck = "text1text2text3";

string[] stringArray = { "text1", "someothertext", etc... };

if(stringToCheck.contains stringArray) //one of the items?
{

}

How can I check if the string value for 'stringToCheck' contains a word in the array?

NetMage
  • 26,163
  • 3
  • 34
  • 55
Theomax
  • 6,584
  • 15
  • 52
  • 72
  • 1
    This blog benchmarks numerous techniques for testing if a string contains a string: http://blogs.davelozinski.com/curiousconsultant/csharp-net-fastest-way-to-check-if-a-string-occurs-within-a-string – Robert Harvey Oct 02 '13 at 00:45
  • This is a prime candidate for a meta question about *highly upvoted incorrect answers*. Is there already one? – Peter Mortensen Apr 21 '22 at 22:26

32 Answers32

984

Here's how:

using System.Linq;

if(stringArray.Any(stringToCheck.Contains))

/* or a bit longer: (stringArray.Any(s => stringToCheck.Contains(s))) */

This checks if stringToCheck contains any one of substrings from stringArray. If you want to ensure that it contains all the substrings, change Any to All:

if(stringArray.All(stringToCheck.Contains))
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • 2
    @Spooks Linq To Objects (which is used in the answer's string-check) can be used via LinqBridge on .NET 2.0 http://www.albahari.com/nutshell/linqbridge.aspx – David Rettenbacher Aug 24 '12 at 20:12
  • 1
    how would you do this with case invariance? – Offler Jun 11 '13 at 07:24
  • 23
    @Offler That would be `stringArray.Any(s => s.IndexOf(stringToCheck, StringComparison.CurrentCultureIgnoreCase) > -1)` – Anton Gogolev Jun 11 '13 at 09:20
  • 1
    My edits to this post are always rejected but just to add that you can also use the same syntax of `Any()` on `All()`, i.e. `if(stringArray.All(stringToCheck.Contains))`. – Scotty.NET Jul 19 '13 at 10:55
  • 1
    Handles comparison badly if one of the strings is empty. So not recommended. – Karlth Nov 01 '13 at 13:21
  • @Spooks On .NET version 2.0, you can use (with an array type like here) `if (Array.Exists(stringArray, stringToCheck.Contains))` and `if (Array.TrueForAll(stringArray, stringToCheck.Contains))`, respectively. The `List<>` type has methods with the same names, but these are instance methods. – Jeppe Stig Nielsen Sep 21 '15 at 10:14
  • @AntonGogolev You seem to accidentally swap the roles of `stringToCheck` and `s`? – Jeppe Stig Nielsen Sep 21 '15 at 10:25
  • if required to compare whole string with each element in string array , then this code gives incorrect result. e.g. stringToCheck = docx , stringArray [doc, docy]. – Raghu Mar 14 '17 at 09:58
  • @Offler An alternative I see no one mentioning is to just put s as lowercase and .Contains it. – Ma Dude Jun 06 '18 at 15:12
180

Here is how you can do it:

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

Maybe you are looking for a better solution... Refer to Anton Gogolev's answer which makes use of LINQ.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133
  • 5
    Thanks, I modified your code to: if (stringToCheck.Contains(s)) and it worked. – Theomax May 26 '10 at 13:29
  • 5
    I did if (stringArray.Contains(stringToCheck)) and it works great, thanks. – Tamara JQ Oct 19 '11 at 14:18
  • 87
    Don't use this answer use LINQ instead – AlexC Mar 23 '12 at 11:19
  • 13
    Little note to people who do not see **Contains** method on the string array: Check if you have a "using System.Linq;" namespace in your codefile :) – Sudhanshu Mishra Apr 05 '12 at 10:33
  • 5
    Linq isn't always available in legacy software. – William Morrison Aug 07 '13 at 17:17
  • 1
    .Net 1.x software should be updated to at least .Net 2.x by now. – NetMage Jun 19 '14 at 17:52
  • 1
    The question was to check if the string contains a word a in the string array not the other way around. – snit80 Sep 30 '16 at 00:35
  • I submitted a modification to fix this answer to be right – NetMage Oct 06 '16 at 23:45
  • @NetMage, for some reason, reviews keep rejecting the edit suggestions... I have made the necessary updates to the answer today – Abdel Raoof Olakara Oct 08 '16 at 19:37
  • using contains is not the best solution here. when checking if the stringToCheck contains x, it would return true for all array items that can be found in text1. so for example stringArray= {"text", "text1", "1" } would all return true. better to use the equal method for an exact match. unless you really just wanted to know if the array item can be found in the string, but the question isn't all that clear tbh. – user3468711 May 11 '21 at 15:59
52
⚠️ Note: this does not answer the question asked
The question asked is "how can I check if a sentence contains any word from a list of words?"
This answer checks if a list of words contains one particular word

Try this:

No need to use LINQ

if (Array.IndexOf(array, Value) >= 0)
{
    //Your stuff goes here
}
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
Maitrey684
  • 762
  • 5
  • 9
  • Nice! And what benefit could Linq possibly have over Array.IndexOf?? – Heckflosse_230 Nov 06 '13 at 20:19
  • 29
    This doesn't solve the question at all. IndexOf tells you if an array contains an exact match for a string, the original question is if a string contains one of an array of strings, which Linq handles easily. – NetMage Jun 19 '14 at 17:53
  • 1
    I know this comment is late, but just to those who don't know, a string is an array of characters so string types do contain an IndexOf method... so @NetMage it is a possible solution. – Blacky Wolf Jun 13 '16 at 14:45
  • 5
    @Blacky Wolf, Did you read the question? Array.IndexOf tells you if an array contains a value, the OP wanted to know if a value contains any member of an array, exactly the opposite of this answer. You could use String.IndexOf with Linq: `stringArray.Any(w => stringToCheck.IndexOf(w) >= 0)` but the Linq answer using String.Contains makes more sense, as that is exactly what is being asked for. – NetMage Oct 06 '16 at 23:51
51
⚠️ Note: this does not answer the question asked
The question asked is "how can I check if a sentence contains any word from a list of words?"
This answer checks if a list of words contains one particular word

Just use the LINQ method:

stringArray.Contains(stringToCheck)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Legolas21
  • 689
  • 5
  • 6
9
⚠️ Note: this does not answer the question asked
The question asked is "how can I check if a sentence contains any word from a list of words?"
This answer checks if a list of words contains one particular word

The easiest and simplest way:

bool bol = Array.Exists(stringarray, E => E == stringtocheck);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lwin
  • 3,784
  • 6
  • 25
  • 46
  • better is stringarray.Exists(entity => entity == stringtocheck) – Marcel Grüger Dec 14 '16 at 15:26
  • 1
    I think you cant call exists method directly from string array.Exists method can use directly for list.So should use static method array.exist for string array.check here => https://msdn.microsoft.com/en-us/library/yw84x8be(v=vs.110).aspx – lwin Dec 15 '16 at 04:24
8
⚠️ Note: this does not answer the question asked
The question asked is "how can I check if a sentence contains any word from a list of words?"
This answer checks if a list of words contains one particular word
string strName = "vernie";
string[] strNamesArray = { "roger", "vernie", "joel" };

if (strNamesArray.Any(x => x == strName))
{
   // do some action here if true...
}
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
Vernie Namca
  • 115
  • 1
  • 2
4

Something like this perhaps:

string stringToCheck = "text1text2text3";
string[] stringArray = new string[] { "text1" };
if (Array.Exists<string>(stringArray, (Predicate<string>)delegate(string s) { 
    return stringToCheck.IndexOf(s, StringComparison.OrdinalIgnoreCase) > -1; })) {
    Console.WriteLine("Found!");
}
Fredrik Johansson
  • 3,477
  • 23
  • 37
  • This is a better solution, since it's a substring check against words in a list instead of an exact match check. – Roy B Mar 10 '15 at 19:04
  • Nice answer, but wow that is hard to read compared to modern C# even without Linq; also, `String.Contains` might be better than `String.IndexOf` unless you want to ignore case, since Microsoft forgot a two argument `String.Contains` you have to write your own. Consider: `Array.Exists(stringArray, s => stringToCheck.IndexOf(s, StringComparison.OrdinalIgnoreCase) > -1)` – NetMage Oct 07 '16 at 00:00
4

You can define your own string.ContainsAny() and string.ContainsAll() methods. As a bonus, I've even thrown in a string.Contains() method that allows for case-insensitive comparison, etc.

public static class Extensions
{
    public static bool Contains(this string source, string value, StringComparison comp)
    {
        return source.IndexOf(value, comp) > -1;
    }

    public static bool ContainsAny(this string source, IEnumerable<string> values, StringComparison comp = StringComparison.CurrentCulture)
    {
        return values.Any(value => source.Contains(value, comp));
    }

    public static bool ContainsAll(this string source, IEnumerable<string> values, StringComparison comp = StringComparison.CurrentCulture)
    {
        return values.All(value => source.Contains(value, comp));
    }
}

You can test these with the following code:

    public static void TestExtensions()
    {
        string[] searchTerms = { "FOO", "BAR" };
        string[] documents = {
            "Hello foo bar",
            "Hello foo",
            "Hello"
        };

        foreach (var document in documents)
        {
            Console.WriteLine("Testing: {0}", document);
            Console.WriteLine("ContainsAny: {0}", document.ContainsAny(searchTerms, StringComparison.OrdinalIgnoreCase));
            Console.WriteLine("ContainsAll: {0}", document.ContainsAll(searchTerms, StringComparison.OrdinalIgnoreCase));
            Console.WriteLine();
        }
    }
Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66
3

Using LINQ and a method group would be the quickest and more compact way of doing this.

var arrayA = new[] {"element1", "element2"};
var arrayB = new[] {"element2", "element3"};

if (arrayB.Any(arrayA.Contains)) 
    return true;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jun Zheng
  • 677
  • 1
  • 15
  • 31
3

If stringArray contains a large number of varied length strings, consider using a Trie to store and search the string array.

public static class Extensions
{
    public static bool ContainsAny(this string stringToCheck, IEnumerable<string> stringArray)
    {
        Trie trie = new Trie(stringArray);
        for (int i = 0; i < stringToCheck.Length; ++i)
        {
            if (trie.MatchesPrefix(stringToCheck.Substring(i)))
            {
                return true;
            }
        }

        return false;
    }
}

Here is the implementation of the Trie class

public class Trie
{
    public Trie(IEnumerable<string> words)
    {
        Root = new Node { Letter = '\0' };
        foreach (string word in words)
        {
            this.Insert(word);
        }
    }

    public bool MatchesPrefix(string sentence)
    {
        if (sentence == null)
        {
            return false;
        }

        Node current = Root;
        foreach (char letter in sentence)
        {
            if (current.Links.ContainsKey(letter))
            {
                current = current.Links[letter];
                if (current.IsWord)
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        return false;
    }

    private void Insert(string word)
    {
        if (word == null)
        {
            throw new ArgumentNullException();
        }

        Node current = Root;
        foreach (char letter in word)
        {
            if (current.Links.ContainsKey(letter))
            {
                current = current.Links[letter];
            }
            else
            {
                Node newNode = new Node { Letter = letter };
                current.Links.Add(letter, newNode);
                current = newNode;
            }
        }

        current.IsWord = true;
    }

    private class Node
    {
        public char Letter;
        public SortedList<char, Node> Links = new SortedList<char, Node>();
        public bool IsWord;
    }

    private Node Root;
}

If all strings in stringArray have the same length, you will be better off just using a HashSet instead of a Trie

public static bool ContainsAny(this string stringToCheck, IEnumerable<string> stringArray)
{
    int stringLength = stringArray.First().Length;
    HashSet<string> stringSet = new HashSet<string>(stringArray);
    for (int i = 0; i < stringToCheck.Length - stringLength; ++i)
    {
        if (stringSet.Contains(stringToCheck.Substring(i, stringLength)))
        {
            return true;
        }
    }

    return false;
}
tcb
  • 4,408
  • 5
  • 34
  • 51
3
⚠️ Note: this does not answer the question asked
The question asked is "how can I check if a sentence contains any word from a list of words?"
This answer checks if a list of words contains one particular word
  stringArray.ToList().Contains(stringToCheck)
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
2

LINQ:

arrray.Any(x => word.Equals(x));

This is to see if array contains the word (exact match). Use .Contains for the substring, or whatever other logic you may need to apply instead.

3xCh1_23
  • 1,491
  • 1
  • 20
  • 39
1

You can also do the same thing as Anton Gogolev suggests to check if any item in stringArray1 matches any item in stringArray2:

using System.Linq;
if(stringArray1.Any(stringArray2.Contains))

And likewise all items in stringArray1 match all items in stringArray2:

using System.Linq;
if(stringArray1.All(stringArray2.Contains))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Scotty.NET
  • 12,533
  • 4
  • 42
  • 51
1

I used a similar method to the IndexOf by Maitrey684 and the foreach loop of Theomax to create this. (Note: the first 3 "string" lines are just an example of how you could create an array and get it into the proper format).

If you want to compare 2 arrays, they will be semi-colon delimited, but the last value won't have one after it. If you append a semi-colon to the string form of the array (i.e. a;b;c becomes a;b;c;), you can match using "x;" no matter what position it is in:

bool found = false;
string someString = "a-b-c";
string[] arrString = someString.Split('-');
string myStringArray = arrString.ToString() + ";";

foreach (string s in otherArray)
{
    if (myStringArray.IndexOf(s + ";") != -1) {
       found = true;
       break;
    }
}

if (found == true) { 
    // ....
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vapcguy
  • 7,097
  • 1
  • 56
  • 52
  • But [Maitrey684 does not answer](https://stackoverflow.com/questions/2912476/using-c-sharp-to-check-if-string-contains-a-string-in-string-array/17425159#17425159) the question(?). – Peter Mortensen Apr 21 '22 at 22:17
  • @PeterMortensen Point was they were on the right track, to use `IndexOf`, and was simply giving credit to where my idea stemmed from. In other words, you can use the code I supplied to take the array, iterate over it, then do the `IndexOf` (like Maitrey684 said, sort of) to tell if it contains your value. – vapcguy Apr 22 '22 at 21:04
1
⚠️ Note: this does not answer the question asked
The question asked is "how can I check if a sentence contains any word from a list of words?"
This answer checks if a list of words contains one particular word

I would use LINQ, but it still can be done through:

new[] {"text1", "text2", "etc"}.Contains(ItemToFind);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
1

To complete the previous answers, for the IgnoreCase check, use:

stringArray.Any(s => stringToCheck.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) > -1)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shadi Serhan
  • 309
  • 3
  • 9
1

For my case, the above answers did not work. I was checking for a string in an array and assigning it to a boolean value. I modified Anton Gogolev's answer and removed the Any() method and put the stringToCheck inside the Contains() method.

bool isContain = stringArray.Contains(stringToCheck);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matthew Miranda
  • 138
  • 1
  • 3
  • 15
1

Using Find or FindIndex methods of the Array class:

if(Array.Find(stringArray, stringToCheck.Contains) != null) 
{ 
}
if(Array.FindIndex(stringArray, stringToCheck.Contains) != -1) 
{ 
}
Andriy Tolstoy
  • 5,690
  • 2
  • 31
  • 30
1

Most of those solutions are correct, but if you need to check values without case sensitivity:

using System.Linq;
...
string stringToCheck = "text1text2text3";
string[] stringArray = { "text1", "someothertext"};

if(stringArray.Any(a=> String.Equals(a, stringToCheck, StringComparison.InvariantCultureIgnoreCase)) )
{
   //contains
}

if (stringArray.Any(w=> w.IndexOf(stringToCheck, StringComparison.InvariantCultureIgnoreCase)>=0))
{
   //contains
}

dotNetFiddle example

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mr R
  • 997
  • 2
  • 12
  • 25
1

You can try this solution as well.

string[] nonSupportedExt = { ".3gp", ".avi", ".opus", ".wma", ".wav", ".m4a", ".ac3", ".aac", ".aiff" };
        
bool valid = Array.Exists(nonSupportedExt,E => E == ".Aac".ToLower());
Dharman
  • 30,962
  • 25
  • 85
  • 135
0
⚠️ Note: this does not answer the question asked
The question asked is "how can I check if a sentence contains any word from a list of words?"
This answer checks if a list of words contains one particular word

I use the following in a console application to check for arguments

var sendmail = args.Any( o => o.ToLower() == "/sendmail=true");
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
bartburkhardt
  • 7,498
  • 1
  • 18
  • 14
0
string [] lines = {"text1", "text2", "etc"};

bool bFound = lines.Any(x => x == "Your string to be searched");

bFound is set to true if the searched string is matched with any element of array 'lines'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pabitra Dash
  • 1,461
  • 2
  • 21
  • 28
0

Try:

String[] val = { "helloword1", "orange", "grape", "pear" };
String sep = "";
string stringToCheck = "word1";

bool match = String.Join(sep,val).Contains(stringToCheck);
bool anothermatch = val.Any(s => s.Contains(stringToCheck));
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Valko
  • 340
  • 2
  • 10
0
string word = "XRC"; // Try also with word = null; 

string[] myWords = { "CUS", "CT", "NMS", "RX", "RUS", "VUS", "XRC", null };

if (myWords.Contains(word))
    Console.WriteLine("1. if: contains the string");
    
if (myWords.Any(word.Contains))
    Console.WriteLine("2. if: contains the string, but null elements throws an exception");
    
if (myWords.Any(x => x.Equals(word)))
    Console.WriteLine("3. if: contains the string, but null elements throws an exception ");
-1
public bool ContainAnyOf(string word, string[] array) 
    {
        for (int i = 0; i < array.Length; i++)
        {
            if (word.Contains(array[i]))
            {
                return true;
            }
        }
        return false;
    }
nakisa
  • 1
-1

Try this

string stringToCheck = "text1text2text3";
string[] stringArray = new string[] { "text1" };

var t = lines.ToList().Find(c => c.Contains(stringToCheck));

It will return you the line with the first incidence of the text that you are looking for.

-1

A simple solution that does not require any LINQ:

String.Join(",", array).Contains(Value + ",");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-1
int result = Array.BinarySearch(list.ToArray(), typedString, StringComparer.OrdinalIgnoreCase);
amit jha
  • 398
  • 2
  • 6
  • 22
-1

Try this. There isn't any need for a loop..

string stringToCheck = "text1";
List<string> stringList = new List<string>() { "text1", "someothertext", "etc.." };
if (stringList.Exists(o => stringToCheck.Contains(o)))
{

}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amjad Abu Saa
  • 1,644
  • 2
  • 15
  • 11
-1

Try this. Example: To check if the field contains any of the words in the array. To check if the field (someField) contains any of the words in the array.

String[] val = { "helloword1", "orange", "grape", "pear" };

Expression<Func<Item, bool>> someFieldFilter = i => true;

someFieldFilter = i => val.Any(s => i.someField.Contains(s));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vijay
  • 7
  • 1
-2

I used the following code to check if the string contained any of the items in the string array:

foreach (string s in stringArray)
{
    if (s != "")
    {
        if (stringToCheck.Contains(s))
        {
            Text = "matched";
        }
    }
}
radbyx
  • 9,352
  • 21
  • 84
  • 127
Theomax
  • 6,584
  • 15
  • 52
  • 72
  • 4
    This sets `Text = "matched"` as many times as `stringToCheck` contains substrings of `stringArray`. You may want to put a `break` or `return` after the assignment. – Dour High Arch May 26 '10 at 13:55
-3

Three options demonstrated. I prefer to find the third as the most concise.

class Program {
    static void Main(string[] args) {
    string req = "PUT";
    if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
        Console.WriteLine("one.1.A");  // IS TRUE
    }
    req = "XPUT";
    if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
        Console.WriteLine("one.1.B"); // IS TRUE
    }
    req = "PUTX";
    if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
        Console.WriteLine("one.1.C");  // IS TRUE
    }
    req = "UT";
    if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
        Console.WriteLine("one.1.D"); // false
    }
    req = "PU";
    if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
        Console.WriteLine("one.1.E"); // false
    }
    req = "POST";
    if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
        Console.WriteLine("two.1.A"); // IS TRUE
    }
    req = "ASD";
    if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
        Console.WriteLine("three.1.A");  // false
    }


    Console.WriteLine("-----");
    req = "PUT";
    if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0)  {
        Console.WriteLine("one.2.A"); // IS TRUE
    }
    req = "XPUT";
    if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0)  {
        Console.WriteLine("one.2.B"); // false
    }
    req = "PUTX";
    if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0)  {
        Console.WriteLine("one.2.C"); // false
    }
    req = "UT";
    if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0)  {
        Console.WriteLine("one.2.D"); // false
    }
    req = "PU";
    if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0)  {
        Console.WriteLine("one.2.E"); // false
    }
    req = "POST";
    if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0)  {
        Console.WriteLine("two.2.A");  // IS TRUE
    }
    req = "ASD";
    if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0)  {
        Console.WriteLine("three.2.A");  // false
    }

    Console.WriteLine("-----");
    req = "PUT";
    if ((new string[] {"PUT", "POST"}.Contains(req)))  {
        Console.WriteLine("one.3.A"); // IS TRUE
    }
    req = "XPUT";
    if ((new string[] {"PUT", "POST"}.Contains(req)))  {
        Console.WriteLine("one.3.B");  // false
    }
    req = "PUTX";
    if ((new string[] {"PUT", "POST"}.Contains(req)))  {
        Console.WriteLine("one.3.C");  // false
    }
    req = "UT";
    if ((new string[] {"PUT", "POST"}.Contains(req)))  {
        Console.WriteLine("one.3.D");  // false
    }
    req = "PU";
    if ((new string[] {"PUT", "POST"}.Contains(req)))  {
        Console.WriteLine("one.3.E");  // false
    }
    req = "POST";
    if ((new string[] {"PUT", "POST"}.Contains(req)))  {
        Console.WriteLine("two.3.A");  // IS TRUE
    }
    req = "ASD";
    if ((new string[] {"PUT", "POST"}.Contains(req)))  {
        Console.WriteLine("three.3.A");  // false
    }

    Console.ReadKey();
    }
}
Steve
  • 905
  • 1
  • 8
  • 32