Edwin Coronado Edwin Coronado - 1 month ago 5x
C# Question

Match all of desired characters in string even when they are not next to each other

I am iterating through a list of words and I need to find words that contain ALL of desired characters. I know how to find a substring but that find words that have the characters next to each other. I want to create something that determines if the string contains all of the characters even when they are not next to each other.

For example if I have a string "ent", words in the list like "element", "nintendo", "telephone" would show up.

I currently something have this logic:

String textLine = "element";
Regex regX = new Regex("e|n|t");
bool containsAny = regX.IsMatch(textLine);

This currently returns true if ANY of the characters exist in the string. I want to create a Regex (or anything else) that will find words that match ALL desired characters. I'm writing this in C#.



You can use Linq

var desiredChars = "ent";
var word = "element";
bool contains = desiredChars.All(word.Contains);