2013-10-09 90 views
0

我使用下面的代碼,但它給了我數組的前10行。我不知道如何獲得數組的10行。讀取數組vb.net中最後一個X行數

 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) 

回答

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) 

我用了兩個variabile的(t_low_1t_low_2),因爲我不明白你想要顯示結果的順序。

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) 
+0

它或者是這個,或者是使用步驟-1與l_low.Length-1中的起始c一起使用。 – WozzeC

+0

感謝dhabal現在會嘗試 – user2835653

+0

dhaval,謝謝你的回覆 - 那給了我10個記錄從列表的最下面10個,而不是最後的10個 – user2835653

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 
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元素。