如何在Visual Basic数组中找到最长的单词?

时间:2013-11-20 16:18:47

标签: arrays vb.net word

我在Visual Basic中有一个数组需要返回最长的单词。如何定位String数组中最长的单词?非常感谢任何帮助!

4 个答案:

答案 0 :(得分:7)

LINQ替代方案是:

Dim strings = New String() {"1", "02", "003", "0004", "00005"}

Dim longest As String = strings.OrderByDescending(Function(s) s.Length).FirstOrDefault()

答案 1 :(得分:3)

Dim longestWord = String.Empty

For Each word in strArray
    If Not String.IsNullOrEmpty(word) AndAlso word.Length > longestWord.Length Then
        longestWord = word
    End If        
Next

** 已更新为占用空字符串 **

答案 2 :(得分:1)

像这样......(这是c#,但应该很容易移植到VB)

string[] stringArray = new string[] { "One", "Two", "Three", "Four" };
string longest = stringArray.OrderByDescending(x => x.Length).FirstOrDefault();

答案 3 :(得分:1)

如果我在C#中使用Linq执行此操作,我会这样做:

var strings = new string[3] { "abc", "defg", "hijkl" };
string longest = strings.OrderByDescending(s => s.Length).FirstOrDefault();