.net - Using predicate to find strings that start with a specific letter (C#) -
i'm quite new using predicates finding specific within collection. here's example:
static void findpersons(string firstletter) { list<string> names = new list<string>() {"marcus", "john", "jesse", "lance", "aaron", "archibald", "victor" }; list<string> names2 = names.findall(a => a.startswith(firstletter)); foreach (var name in names2) { console.writeline(name); } }
i'd call method within main:
findpersons("a");
i had use string first letter because startswith takes string parameter (or use char , tostring().
how can using shorter syntax? 1 more question - generally, how during work use predicates find specific objects within collection? thanks.
just use console.writeline(string.join<string>("\n", result));
following method that.
static void findpersons(string firstletter) { list<string> names = new list<string>() {"marcus", "john", "jesse", "lance", "aaron", "archibald", "victor" }; list<string> result = names.where(a => a.startswith(firstletter, stringcomparison.invariantcultureignorecase)).tolist(); console.writeline(string.join<string>("\n", result)); }
Comments
Post a Comment