2013-02-06 42 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