How to find the longest word in a string with VB.NET

2 Answers

0 votes
Imports System
Imports System.Linq

Module Module1

    Function LongestWord(s As String) As String
        Dim words = s.Split({" "}, StringSplitOptions.RemoveEmptyEntries)

        Return words.OrderByDescending(Function(w) w.Length).First()
    End Function

    Sub Main()
        Console.WriteLine(LongestWord("Could you recommend a good restaurant nearby?"))
    End Sub

End Module

	
' run:
'
' restaurant
'

 



answered Mar 2 by avibootz
0 votes
Imports System

Module Module1

Function LongestWord(s As String) As String
    Dim words = s.Split({" "}, StringSplitOptions.RemoveEmptyEntries)
    Dim longest As String = ""

    For Each w In words
        If w.Length > longest.Length Then
            longest = w
        End If
    Next

    Return longest
End Function


    Sub Main()
        Console.WriteLine(LongestWord("Could you recommend a good restaurant nearby?"))
    End Sub

End Module

	
' run:
'
' restaurant
'

 



answered Mar 2 by avibootz
...