Is there any way to search patterns in strings in C#?
Something like Sql LIKE would be very useful.
public static class StringsEx
{
public static IEnumerable Like(this IEnumerable input, String pattern)
{
var dt = new DataTable();
dt.Columns.Add("Search");
foreach (String str in input)
{
dt.Rows.Add(str);
}
dt.DefaultView.RowFilter = String.Format("Search LIKE '{0}'", pattern);
return dt.DefaultView.ToTable()
.AsEnumerable()
.Select(r => r.Field("Search"));
}
}
The only disadvantage is following: "Wildcard characters are not allowed in the middle of a string. For example, 'te*xt' is not allowed."©