How to find the shortest string in array of strings in VB.NET

3 Answers

0 votes
Imports System
				
Public Module Module1
	Public Sub Main()
		Dim arr() As String = {"c++", "python", "c#", "java"}
		Dim shortest_string As String = arr(0)
		
		For Each s As String In arr
  			If s.Length < shortest_string.Length Then
    			shortest_string = s
  			End If
		Next
		
		Console.Write(shortest_string)
	End Sub
End Module



' run:
'  
' c#
'  

 



answered Mar 7, 2021 by avibootz
edited Mar 7, 2021 by avibootz
0 votes
Imports System
                 
Public Module Module1
    Public Sub Main()
        Dim arr() As String = {"c++", "python", "c#", "java"}
		Dim shortest_string As String = arr(0)
 
        For Each s in arr
            If Not String.IsNullOrEmpty(s) AndAlso s.Length < shortest_string.Length Then
                shortest_string = s
            End If       
        Next
         
        Console.Write(shortest_string)
    End Sub
End Module
 
 
 
' run:
'  
' c#
'

 



answered Mar 7, 2021 by avibootz
0 votes
Imports System
Imports System.Linq

Public Module Module1
    Public Sub Main()
        Dim arr() As String = {"c++", "python", "c#", "java"}

		Dim shortest_string As String = arr.OrderByDescending(Function(s) s.Length).LastOrDefault()
         
        Console.Write(shortest_string)
    End Sub
End Module
 
 
	
 
' run:
'  
' c#
'  

 



answered Mar 7, 2021 by avibootz
...