Reputation: 89
I'm having a list of string like
var target = new List<string>() { "C", "C-sharp", "java" };
I'm having a string request = "C is a programming language"
This list should match with the string and should return
C,C-sharp
How can i do this?
Upvotes: 0
Views: 181
Reputation: 7470
here is the solution with linq
var m = from t in target
where t[0] == 'C'
select t;
Upvotes: 3
Reputation: 138007
Using Linq and String.Contains
:
var filtered = target.Where(str => str.Contains("C"));
Another option, without Linq, is to change the existing list using List<T>.RemoveAll
:
target.RemoveAll(str => !str.Contains("C"));
If you really need a regex (for something more complex), you may also use:
Regex validate = new Regex(".a.", RegexOptions.IgnoreCase);
var filtered = target.Where(str => validate.Match(str).Success);
Upvotes: 2