首页 > 解决方案 > 如何在没有逗号的情况下结束 for 循环?VB.NET

问题描述

我正要使用 for 循环从一个单元格中保存每个数字,这是我的代码:

For Each node As XmlNode In xmlnode
                    Dim X As String
                    Dim Y As String
                    Dim Z As String
                    X = node.SelectSingleNode("X").InnerText
                    Y = node.SelectSingleNode("Y").InnerText
                    Z = node.SelectSingleNode("Z").InnerText

                    Dim noOfPts As Integer = 0  'number of data points in data
                    For i As Double = 0 To noOfPts
                        result = X + ","
                        result2 = Y + ","

                    Next
      Next

问题是我将在不保存逗号的情况下结束循环。而不是这个“1,2,3,4,5”,应该像这样“1,2,3,4,5”从循环末尾删除逗号。

标签: vb.net

解决方案


我喜欢这样做:

For i As Double = 0 To noOfPts
    If result <> "" Then result &= ", "
    result &= X

    If result2 <> "" Then result2 &= ", "
    result2 &= Y
Next

玩得开心!


推荐阅读