Edwin Coronado
Edwin Coronado

Reputation: 69

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#.

Thanks!

Upvotes: 0

Views: 251

Answers (1)

L.B
L.B

Reputation: 116168

You can use Linq

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

Upvotes: 1

Related Questions