c# - Linq Expression Get Predicated Items From Last With a Function -
are there linq
expression exists gives predicated list end of source list
.
i.e: "abc1zxc".tochararray().somemagiclinq(p=>char.isletter(p));
should give "zxc"
you use approach:
var lastletters = "abc1zxc".reverse().takewhile(char.isletter).reverse(); string lastlettersstring = new string(lastletters.toarray());
not efficient way working , readable.
if need single (optimized) method use this:
public static ienumerable<tsource> getlastpart<tsource>(this ienumerable<tsource> source, func<tsource, bool> predicate) { var buffer = source ilist<tsource> ?? source.tolist(); var reverselist = new list<tsource>(); (int = buffer.count - 1; >= 0; i--) { if (!predicate(buffer[i])) break; reverselist.add(buffer[i]); } (int = reverselist.count - 1; >= 0; i--) { yield return reverselist[i]; } }
then it's more concise:
string lastletters = new string("abc1zxc".getlastpart(char.isletter).toarray());
Comments
Post a Comment