c# - Checking if a string contains words in a specific order -
here's interesting question, have list contains 3 sentences such as:
bill cat had
bill had cat
cat had bill
how utilize .contains() or other method check if sentences in list contains words in specific order, algorithm shown below:
1) run sentence list through foreach loop
2) check if sentence contains words in order => bill + had + cat
3) homecoming sentence
so every other sentence returned false since order of words different. ideas on implementing this, folks? :)
try below solution works.
class programme { static void main(string[] args) { list<string> list = new list<string>(); list.add("bill cat had"); list.add("bill had cat"); list.add("bill had cat"); list.add("cat had bill"); regex rex = new regex(@"((bill)).*((had)).*((cat))"); foreach (string str in list) { if (rex.ismatch(str)) { console.writeline(str); } } console.readline(); } }
c# string foreach order condition
No comments:
Post a Comment