Ebikeneser
Ebikeneser

Reputation: 2364

Check if words of a string is present in a string list

I have the following code that works a treat -

List<string> test = new List<string>();
test.Add("cat");

if (test.Any(str => str.Contains("cat")))
{
      // do something
};

However is there a way to check if there is not an exact match for instance -

if (test.Any(str => str.Contains("the cat sat")))
{
      // do something
};

I want to be able to check if any of the words in the string are present in the list. How can I achieve this?

Upvotes: 2

Views: 475

Answers (5)

Mostafiz
Mostafiz

Reputation: 7352

You can simply do by this way

string check = "the cat sat";
if(test.Any(str => check.Split(' ').Contains(str)))
{
      // present
};

Upvotes: 4

Hamid Pourjam
Hamid Pourjam

Reputation: 20754

You should extract words and check for existence of them in elements of test

var data = "the cat sat";
var words = data.Split(null);
if (test.Any(str => words.Any(word => str.Contains(word)))
{
  // do something
}

If you want to check for exact match you should use word => str == word.

"a cat".Contains("cat") //true
"a cat" == "cat" //false

Upvotes: 5

Gilad Green
Gilad Green

Reputation: 37299

Split your sentence into the different words:

var words = new HashSet<string>("the cat sat".Split(' '));

And then you can:

  1. Check if that collection contains any of the strs:

    var test = new List<string> { "cat" };
    var words = new HashSet<string>("the cat sat".Split(' '));
    
    var result =  test.Any(str => words.Contains(str)); // In this case "true"
    
  2. Use Intersect:

    var test = new List<string> { "cat" };
    var words = new HashSet<string>("the cat sat".Split(' '));
    
    var result = test.Intersect(words).Any();
    

The use of the HashSet is so when you go through the 2 collections it'll in o(n) and not o(n)2

If you do not have exact matches in the test list like in the following example:

var test = new List<string> { "some cat" };

Then you can do something like this:

var test = new List<string> { "some cat" };
var words = "the cat sat".Split(' ').ToList();

var result = words.Any(word => test.Any(item => item.Contains(word)));

Upvotes: 4

ZiNNED
ZiNNED

Reputation: 2650

One way of achieving this would be to make use of an extension to the string object.

public static bool Contains(this string input, bool caseSensitive, params string[] items)
{
    input = !caseSensitive ? input.ToLower() : input;

    foreach (var item in items)
    {
        if (input.Contains(!caseSensitive ? item.ToLower() : item))
            return true;
    }

    return false;
}

Your following line would then also work with a minor change:

if (test.Any(str => str.Contains(true, "the", "cat", "sat"))) // true for case sensitive contains
{
      // do something
};

Upvotes: 2

Mark Atkinson
Mark Atkinson

Reputation: 518

bool contains = false;        
string checkString = "the cat sat";
checkString.Split(' ').ToList().Foreach(x => {
  if (test.Any(str => str.Contains(x)) && !contains)
  {
      // do something
      contains = true;
  };
});

Upvotes: 1

Related Questions