Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Find Longest String from Array Using Lambda Expression in C#



The following is our string array −

string[] arr = { "Java", "HTML", "CSS", "JavaScript"};

Use the Aggregate method and set a Lambda Expression to find the string with more number of characters.

Here, the resultant string should have more number of characters than the initial seed value i.e. “jQuery” here.

Example

 Live Demo

using System;
using System.Linq;
class Demo {
   static void Main() {
      string[] arr = { "Java", "HTML", "CSS", "JavaScript"};
      string res = arr.AsQueryable().Aggregate("jQuery", (longest, next) => next.Length >       longest.Length ? next : longest,str => str.ToLower());
      Console.WriteLine("String with more number of characters: {0}", res);
   }
}

Output

String with more number of characters: javascript
Updated on: 2020-06-23T07:56:57+05:30

918 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements