使用 Lambda 表示式從字串陣列中查詢最長字串的 C# 程式


以下為我們的字串陣列 −

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

使用聚合方法並設定一個 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

更新於: 23-6 月 -2020

807 次瀏覽

Kickstart Your 事業

完成課程以獲得認證

Get Started
廣告