arrays - Array 和 Split 命令创建二维数组

标签 arrays vb6 multidimensional-array split

我在使用 split 命令填充数组时遇到了一些问题。

我目前拥有的字符串在下面

MyString = "Row1 Column1[~]Row1 Column2[~]Row1 Column3" & vbNewLine & _
"Row2 Column1[~]Row2 Column2[~]Row2 Column3" & vbNewLine & _
"Row3 Column1[~]Row3 Column2[~]Row3 Column3" & vbNewLine & _
"Row4 Column1[~]Row4 Column2[~]Row4 Column3"

我有一个数组,我希望它是多维的,并且希望每个 Row# Column# 根据其编号位于数组的正确部分。

例如
MyArray(1,1) = "Row1 Column1"
MyArray(2,1) = "Row2 Column1"
MyArray(3,1) = "Row3 Column1"
MyArray(4,1) = "Row4 Column1"

MyArray(1,2) = "Row1 Column2"
MyArray(2,2) = "Row2 Column2"
MyArray(3,2) = "Row3 Column2"
MyArray(4,2) = "Row4 Column2"

MyArray(1,3) = "Row1 Column3"
MyArray(2,3) = "Row2 Column3"
MyArray(3,3) = "Row3 Column3"
MyArray(4,3) = "Row4 Column3"

现在我明白了如何使用 split 命令填充一维数组
MyArray = Split(MyString, vbNewLine)

这将意味着
MyArray(1) = "Row1 Column1[~]Row1 Column2[~]Row1 Column3"
MyArray(2) = "Row2 Column1[~]Row2 Column2[~]Row2 Column3"
MyArray(3) = "Row3 Column1[~]Row3 Column2[~]Row3 Column3"
MyArray(4) = "Row4 Column1[~]Row4 Column2[~]Row4 Column3"

但我不知道如何使用拆分命令来填充第二维。

这可能吗?如果可能的话?
如果不可能,有人可以建议如何实际填充它吗?

最佳答案

除了 String 或包含 String 的 Variant 之外,您不能对任何其他东西使用 Split()。如果要生成二维字符串数组,您将遍历 Split() 返回的数组,并对每个字符串运行 Split()。下面的函数应该做你想做的:

Private Function SplitTo2DArray(ByRef the_sValue As String, ByRef the_sRowSep As String, ByRef the_sColSep As String) As String()

    Dim vasValue                    As Variant
    Dim nUBoundValue                As Long
    Dim avasCells()                 As Variant
    Dim nRowIndex                   As Long
    Dim nMaxUBoundCells             As Long
    Dim nUBoundCells                As Long
    Dim asCells()                   As String
    Dim nColumnIndex                As Long

    ' Split up the table value by rows, get the number of rows, and dim a new array of Variants.
    vasValue = Split(the_sValue, the_sRowSep)
    nUBoundValue = UBound(vasValue)
    ReDim avasCells(0 To nUBoundValue)

    ' Iterate through each row, and split it into columns. Find the maximum number of columns.
    nMaxUBoundCells = 0
    For nRowIndex = 0 To nUBoundValue
        avasCells(nRowIndex) = Split(vasValue(nRowIndex), the_sColSep)
        nUBoundCells = UBound(avasCells(nRowIndex))
        If nUBoundCells > nMaxUBoundCells Then
            nMaxUBoundCells = nUBoundCells
        End If
    Next nRowIndex

    ' Create a 2D string array to contain the data in <avasCells>.
    ReDim asCells(0 To nUBoundValue, 0 To nMaxUBoundCells)

    ' Copy all the data from avasCells() to asCells().
    For nRowIndex = 0 To nUBoundValue
        For nColumnIndex = 0 To UBound(avasCells(nRowIndex))
            asCells(nRowIndex, nColumnIndex) = avasCells(nRowIndex)(nColumnIndex)
        Next nColumnIndex
    Next nRowIndex

    SplitTo2DArray = asCells()

End Function

例子:
Dim asCells() As String

asCells() = SplitTo2DArray(MyString, vbNewline, "~")

关于arrays - Array 和 Split 命令创建二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10614650/

相关文章:

javascript - 如何在 Object 中搜索在数组中也有值的值?

c - Malloc、可变长度数组还是静态数组?

C++ 控制台问题

python - 如何在Python中操作多维numpy数组

javascript - 如何在循环中将元素推送到多维数组(javascript)?

javascript - 无法使用 Laravel Mix 在 Vue 组件中访问 js-cookie

C程序: Integer array pointer changes values when passed as parameter

windows - VB6 程序不能在 Windows Me 中运行

vb6 - 从系列中删除异常

sql-server - 使用 ADO 时返回成功执行 SQL 的数据库消息