How to find the shortest string size in array of strings in C#

2 Answers

0 votes
using System;
using System.Linq;
  
class Program
{
    static void Main() {
        string[] arr = {"c++", "python", "c#", "java"};
          
        int shortest_string_size = arr.Min(s=>s.Length);

        Console.Write(shortest_string_size);
    }
}
  
  
  
/*
run:
  
2
  
*/

 



answered Mar 7, 2021 by avibootz
0 votes
using System;
using System.Linq;
 
class Program
{
    static void Main() {
        string[] arr = {"c++", "python", "c#", "java"};
         
        int shortest_string_size = (arr.OrderBy(s => s.Length).FirstOrDefault()).Length;
 
        Console.Write(shortest_string_size);
    }
}
 
 
 
/*
run:
 
2
 
*/

 



answered Mar 7, 2021 by avibootz
...