我想要一个有两层的Jagged数组:1)是一个数字,2)是一个字符串数组,所以我可以根据索引值轻松引用一个数组。这一点是获取文本文件的内容,以便锯齿状数组中的每个索引都是文本文件的一行(第1层=索引,第2层=行)。当然,这些行由字符串填充。
没有任何调整,文本文件在每行的字符串之间都有不需要的空间。我希望字符串数组不包含任何浪费的空间("Hello how are you "
- > ["hello","how","are","you"]
)。
我通过Trim
函数和Split
函数执行此操作。 Trim
删除除分隔符之外的所有空格; Split
生成我想要的那个字符串数组。我的问题是将数组放入Jagged数组以及在不知道其长度的情况下创建数组,因为我还没有Split
文本行。
以下是我的代码。当我使用变体而不是String作为我的第二层时,我得到另一个我似乎无法解决的错误。 注意:包含文本文件信息的字符串数组是 tempString()
所以你可以测试一下,使用tempString = (" test tempstring ", "", " test test test", " "," test ", "")
Private Sub createGCStruct(ByRef tempString() As String)
' note many parameters are not included.
' also, this would be a function producing a structure, but for now I just need this to properly create a
' jagged array.
' set variables
Dim tempString2() As String
ReDim tempString2(UBound(tempString()))
Dim j As Integer
Dim jaggArray() As Variant '*****outer layer of the jagged array*****
ReDim jaggArray(UBound(tempString()) + 1)
Dim stringST() As String '*****inner layer of the jagged array*****
Dim tempString4() As String
' set initial values of structure
' ...more code...
' capture structure information from textfile array
' A) remove unnecessary spaces from existing Array
For j = LBound(tempString()) To UBound(tempString())
' check to see if line is zero length string
If tempString(j) = "" Then
' what you don't see are my commented out, futile attempts at
' solving this problem; just know that they exist
Erase stringST
stringST = tempString(j)
jaggArray(j + 1) = stringST
' trim excesive spacing
Else
tempString2(j) = Trim(tempString(j))
Erase stringST
stringST = Split(tempString2(j), " ")
jaggArray(j + 1) = stringST
End If
Next j
'Below is me testing to see if this works'
tempString4 = jaggArray(1)
MsgBox tempString4(0), vbExclamation, "test"
' B) Add sections from array to structure
'... more code...
End Sub
答案 0 :(得分:1)
我可以看到代码中您尝试处理Split
函数的奇怪bugfeature行为,即分割空字符串Split("")
时出现“无法分配给数组”错误返回一个完全无法使用的String(0到-1)数组。
“无法分配给数组”错误是由以下行引起的:
stringST = tempString(j)
左边的东西stringST
是一个数组。右边的东西tempString(j)
不是。 (它是来自tempString
数组的一个元素。)您不能将非数组分配给数组,因此会出错。
你可以做的是定义一个包含单个元素的数组,一个空字符串:
Dim emptyStringArrayPlaceholder() As String
ReDim emptyStringArrayPlaceholder(0 To 0)
然后将其用作空字符串的占位符:
stringST = emptyStringArrayPlaceholder
以下是我清理代码的方法:
Private Sub createGCStruct(ByRef tempString() As String)
Dim jaggArray() As Variant '*****outer layer of the jagged array*****
jaggArray = splitStringArrayElements(tempString)
'Below is me testing to see if this works'
tempString4 = jaggArray(1)
MsgBox tempString4(0), vbExclamation, "test"
' B) Add sections from array to structure
'... more code...
End Sub
我在哪里使用这个功能:
Private Function splitStringArrayElements(tempString() As String) As Variant()
Dim j As Long
Dim trimmedString As String
Dim jaggArray() As Variant
ReDim jaggArray(LBound(tempString()) To UBound(tempString()))
Dim emptyStringArrayPlaceholder() As String
ReDim emptyStringArrayPlaceholder(0 To 0)
For j = LBound(tempString()) To UBound(tempString())
trimmedString = Trim(tempString(j))
If trimmedString = "" Then
jaggArray(j) = emptyStringArrayPlaceholder
Else
jaggArray(j) = Split(trimmedString, " ")
End If
Next j
splitStringElements = jaggArray
End Function