I’m trying to allow a function to run if a string Param passed is within a list. Though when using .Contains it doesn’t seem to work how I’d expect:
Var list = new List<string>() { “A RED CAR” };
If(list.contains(“RED”) { //Do stuff };
The issue here is contains returns false even though a string in the list contains a part of it.
What you want:
Oh nice, I assumed the .contains worked similarly on a string list as it does a string, I see you’re checking each individual in the list, should’ve thought of that!
Thank you very much!!
issue here is that you are checking if the list literally contains “RED”, which it doesn’t. It contains “A RED CAR”. you can change it with a lambda to a .Any() and add the string comparrison in the predicate. Like this:
Brilliant, thank you for the answer!
I made the assumption that contains already looped inside of the list.
You’ve already got the answer here.
But I don’t think anyone has clearly explained to you why it doesn’t work the way you tried it.
Your list contains only one string: “A RED CAR”. It does not contain 3 strings.
The following would work:
This time, the list contains 3 strings, each separated by a comma. One of those strings is “RED”, so therefore the list contains “RED”.
So how do you go from { "A RED CAR" }
to { "A", "RED", "CAR" }
? Well, you could split your original string every time you find a space. So this would work:
But although that would work, and it’s fine if you only have one string to split into words, it’s a bit awkward to split every string in your list, if you have many strings.
The answers that have been proposed, then, are not checking if the list contains “RED”. What they are doing is checking is Any item in the list has a string which contains “RED”. The .Contains()
method you used is a method on the List class. The .Contains()
method that’s been suggested by /u/WetSound and others is a method on the String class – you’re not asking the list if it contains something, you’re asking the string if it contains something, and then checking with the list whether any of the strings say Yes to that.
Also – the solution proposted by /u/WetSound may not be quite what you want. For example, this will do stuff:
because the word BORED contains the letters RED. For this reason, I’d choose the following solution instead:
This splits each of the many strings into words (using .Split) and combines them all into a single list of words (using SelectMany). Then it checks if that list of words contains your word – the .Contains in this example is a method that works on the list (as per your original attempt), not on the string.
(Edit to add my proposed solution)
its because “A RED CAR” is a whole string, and .Contains is checking to see if the list contains an exact element, so a string. it’s checking if the list contains exactly “RED”, and it wont because “RED” is not the same as “A RED CAR” from an object’s point of view.
what you can do instead is:
As a suggestions, maybe use a HashSet instead of a list.
The benefits of a hashset go out the window if you want to filter on the contents of the key instead of the key as a whole. In this case searching the list and checking each entry individually is quicker, as others have noted.
So I did try the Hashset approach but this still doesn’t allow a match on the string using .contains directly on the Hashset.
I used the same strings as my given in my example. Unless you mean to also utilise the loop on the Hashset too?
Other wise thank you for the suggestion, was interesting to read into it and will be using it in the future!
When starting out, one of the most important skills you can pick up is knowing what/how to find the answers you need.
Try helping the guy out rather than flame. No experienced developer would react in the way you have.
Removed: Rule 5.
C# devs
null reference exceptions