I have a string containing up to 9 unique numbers from 1 to 9 (myString) e.g. "12345"
I have a list of strings {"1"}, {"4"} (myList) .. and so on.
I would like to know how many instances in the string (myString) are contained within the list (myList), in the above example this would return 2.
so something like
count = myList.Count(myList.Contains(myString));
I could change myString to a list if required.
Thanks very much
Joe
try
this will only work if the item in myList is a single digit
Edit: as you probably noticed this will convert myString to a char array multiple times so it would be better to have
Try
It is not perfectly clear what you need, but these are some options that could help:
or
the first would return the number of strings in the list that are the same as yours, the second would return the number of strings that contain yours. If neither works, please make your question more clear.
This is one approach, but it's limited to 1 character matches. For your described scenario of numbers from 1-9 this works fine. Notice the
s[0]
usage which refers to the list items as a character. For example, if you had "12" in your list, it wouldn't work correctly.For multiple character matches, which would correctly count the occurrences of "12", the
Regex
class comes in handy:I would try the following:
If
myList
is justList<string>
, then this should work:If
myList
isList<List<string>>
: