读取数组vb.net中的最后X行数

时间:2013-10-09 11:03:04

标签: vb.net

我正在使用下面的代码,但它给了我数组的前10行。我无法弄清楚如何获得数组的最后 10行。

        Dim t_low As String

        Dim l_low() As String = low.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)


        For c As Integer = 0 To l_low.Length - 1
            If c <= 10 - 1 Then
                t_low = t_low & l_low(c) & ","
            End If
        Next
        MsgBox(t_low)

4 个答案:

答案 0 :(得分:1)

    Dim t_low As String

    Dim l_low() As String = low.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)

    Array.Reverse(l_low)

    For c As Integer = 0 To 9            
        t_low = t_low & l_low(c) & ","            
    Next
    MsgBox(t_low)

答案 1 :(得分:0)

我有一个包含一些项目的数组(测试)。此代码连接第一个和最后10个项目,以逗号分隔。

    Dim s As New System.Text.StringBuilder
    If test.Length >= 10 Then
        'first 10
        For c As Integer = 0 To 9
            s.Append(test(c) & ",")
        Next
        'last 10
        For c As Integer = test.Length - 10 To test.Length - 1
            s.Append(test(c) & ",")
        Next
        s.Remove(s.Length - 1, 1) 'remove trailing comma
        Debug.WriteLine(s.ToString)
    End If

答案 2 :(得分:0)

试试这个:

Dim t_low_1 As String = ""
Dim t_low_2 As String = ""

Dim l_low() As String = low.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)

For i As Integer = l_low.Length - 1 To l_low.Length - 10 Step -1
    t_low_1 = t_low_1 & l_low(i) & IIf(i <> l_low.Length - 10, ",", "")
    t_low_2 = l_low(i) & IIf(i <> l_low.Length - 1, ",", "") & t_low_2
Next i

MsgBox(t_low_1)
MsgBox(t_low_2)

我使用了两个变量(t_low_1t_low_2)因为我不明白你想要显示结果的顺序。

答案 3 :(得分:0)

使用LINQ:

Dim l_low() As String = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}
Dim l_low2() As String = l_low.Skip(Math.Max(0, l_low.Length - 10)).Take(10).ToArray

然后,您可以使用String.Join将逗号分隔的l_low2元素加在一起。