2013-02-06 46 views
0

我有一個StringBuilder我正在使用,我從一個循環通過DataGridView添加到它。我需要做的是分離字符串,如果有多於一個「,」。然後我從StringBuilder中設置一個標籤的文本。下面是沒有逗號的工作示例...如何分隔字符串生成器中的字符串?

這是更新版本的偉大工程.... NOW

Dim strUnits As New System.Text.StringBuilder 
    Dim lineCount As Integer = 0 

    For i = 0 To dgvLowInventory.RowCount - 1 
     If dgvLowInventory.Rows(i).Cells(1).Value Is DBNull.Value Or dgvLowInventory.Rows(i).Cells(2).Value Is DBNull.Value Then 
      'Skip 
     Else 
      If lineCount >= 1 Then 
       strUnits.Append(", ") 
       strUnits.Append("[" & dgvLowInventory.Rows(i).Cells(1).Value & " - " & dgvLowInventory.Rows(i).Cells(3).Value & "]") 
       lineCount += 1 
      Else 
       strUnits.Append("[" & dgvLowInventory.Rows(i).Cells(1).Value & " - " & dgvLowInventory.Rows(i).Cells(3).Value & "]") 
       lineCount += 1 
      End If 
     End If 
    Next 

    lblTestString.Text = strUnits.ToString() 

回答

0

保留一個計數器,從0開始。

添加1〜每次追加時,計數器都會追加。

在追加之前,如果計數器大於0,請在追加字符串之前附加", "

或者,使用String.Join和", ",這取決於您正在編程的語言通常與此相當。 http://msdn.microsoft.com/en-us/library/57a79xd0.aspx

+0

謝謝,這就是我已經在努力,只是要把它放在正確的位置。發生了什麼是它會添加字符串之間的逗號,但也是最後一個我不想要的。我編輯了上面的代碼,以顯示我做了什麼並且效果很好。再次感謝! – Codexer