Built-in functions in dotNet 3.0+ with C #?
I'm looking for a trick at new points where I can use built-in functions that return a string value. Here's what I have:
var split = new[] { " " };
var words = SearchTextBox.Text.Trim().Split(
split,
StringSplitOptions.RemoveEmptyEntries);
var textQuery = /*inlinefunction that operates on words array and returns a string.*/
I know I've seen this before, perhaps with chaining methods or anonymous functions ... I just can't remember if I imagined the whole thing or not :-)
source to share
It looks like you are thinking about linq for objects, maybe with .First()
at the end to get the string.
var textQuery = words.Where(w => w.Length > 5).First();
The key to getting all the work done is the lamdba expression and IEnumerable<T>
and its associated extension methods. It is not limited to strings.
source to share
To get a string from a query (or any other IEnumerable), you can use String.Join . Example:
string result = String.Join(" ", textQuery.ToArray());
So, use LINQ like the other answers suggesting to work with "words", and then use String.Join to recompile them to a string.
source to share