无论如何,我可以为每个数组元素修剪最后一个“,”吗?

时间:2012-01-24 12:35:49

标签: vb.net

 While Not sr.EndOfStream
        line = sr.ReadLine
        If line.Contains("Year") Then
            currentYear = line.ToString
        ElseIf line.Contains("mandatory") Then
            moduleStats = "M"
        ElseIf line.Contains("optional") Then
            moduleStats = "O"
        ElseIf line.Contains("COM") Then
            modArray = line.Split(",")
            Dim i As Integer = modArray.Length
            ReDim Preserve modArray(modArray.Length + 2) 'ReDim statement to change the size of one or more dimensions of an array, 
            'Preserve you can resize that dimension and still preserve all the contents of the array

            modArray(i) = moduleStats
            modArray(i + 1) = currentYear.ToString()



            MsgBox(String.Join(",", modArray))

这是我的代码,当我在消息框中查看数组时,它在每个数组元素的末尾都包含一个逗号,这是我不想知道的是我如何为每个元素修剪它?感谢

1 个答案:

答案 0 :(得分:1)

您的ReDim添加了太多的元素。

这会导致Join有额外的,

将其更改为:

ReDim Preserve modArray(modArray.Length + 1)