0

Let's say I have List<string> that looks like this:

  • Apple
  • Pear
  • Peach
  • Plum

Then in a single string, if I have:

I would like to eat a pear today

(Ignore case.) In that case I I would want true because pearis found both in the list and in the string. But if I had a string:

I would like to eat a strawberry today

Then I would get false because none of the List<string>'s are found in the sentence.

I've been playing around with various things like:

string result = Fruits.FirstOrDefault(s => s.IndexOf(sentence) > 0);

Where Fruits is the List<string> and sentence is the other string. Not nailing it.

7
  • So, if you want to have true\false as result, you can use it like string result = Fruits.Any(s => s.IndexOf(sentence) > 0); Commented Sep 28, 2017 at 22:49
  • bool result = ....? I'll give that a shot. Commented Sep 28, 2017 at 22:50
  • I always get 'false' :-( Commented Sep 28, 2017 at 22:52
  • you need to check in this way => string result = Fruits.Any(s => sentence.IndexOf(s) > -1); Commented Sep 28, 2017 at 22:53
  • 1
    if it wasn't clear, the issue was that you were checking if the word contains the sentence instead of if the sentence contains the word. Commented Sep 28, 2017 at 23:14

4 Answers 4

2

Try this:

bool result = Fruits.Any(s => sentence.ToLower().Contains(s.ToLower()));

or

bool result = Fruits.Any(s => sentence.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) >= 0);
Sign up to request clarification or add additional context in comments.

1 Comment

hmm, I thought this looked good, but I'm getting, "string does not contain a definition for "contains" "
1

you need to check in this way =>

string result = Fruits.Any(s => sentence.IndexOf(s) > -1);

or

string result = Fruits.Any(s => sentence.Contains(s));

2 Comments

Any returns a bool not a string.
@Aominè OP wanted to get bool as understand from question I would want true or Then I would get false
1

If you are open to another approach it could be done using the following method:

public static bool ListItemInString(List<string> listOfStrings, string stringToCheck) {
    foreach(string str in listOfStrings) {
        if (stringToCheck.ToUpper().Contains(str.ToUpper())) {
            // return true if any of the list items are found in stringToCheck
            return true;
        }
    }
    // if the method has not returned true by this point then we return false
    // to indicate none of the list items were found within the string
    return false;
}

This method loops through your list and checks each item against your designated string. It will return try if at any point in the foreach loop it finds that one of your list items is contained within the string, otherwise it will return false. It also takes in consideration your non-case sensitive request by converting both strings to uppercase before performing its search through the string.

Edit: I understand this is more of a manual approach. The previous answers on this post could similarly be worked into a method to improve code readability if this is an operation being performed multiple times.

1 Comment

you can use string.Compare overload to specify ignoreCase parameter as true or move this setting to function parameters. But why you think that changing 1 line of code to 5 lines and encapsulate those 5 lines into new function will improve readability? What is not understandable here string result = Fruits.Any(s => sentence.Contains(s)); that you want to have function with comments for it?
1

As per the initial question, if you are looking for matching strings in the sentence and not boolean result following code should help:

List<string> Fruits = new List<string> { "Apple", "Pear", "Peach", "Plum" };
var sentence = "I would like to eat a pear and apple today";
var sentenceLower = sentence.ToLower(); 
var delimiter = ",";
var match = Fruits
  .Where(s => sentenceLower.Contains(s.ToLower()))
  .Aggregate((i, j) => i + delimiter + j);

1 Comment

It would be better for performance to prepare sentence.ToLower() before linq query to not calculate it each time. Or use string.Compare overload and specify ignoreCase parameter as true No much impact, just as suggestion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.