C# 程序使用 Lambda 表达式从字符串数组中查找最长字符串

csharpprogrammingserver side programming更新于 2025/9/14 23:03:00

以下是我们的字符串数组 −

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

使用 Aggregate 方法并设置 Lambda 表达式来查找字符数更多的字符串。

此处,结果字符串的字符数应多于初始种子值,即此处的 “jQuery”。

示例

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);
   }
}

输出

String with more number of characters: javascript

相关文章