Ben,
First:
Put Option Strict On at the top of your source file! As you will receive a
number of compile options that identifies the plethora of errors in your
sample! Using Option Strict On ensure you receive easy to fix compile
errors, rather then hard to find (such as this on) runtime errors.
Second:
SplitFields returns a ragged or jagged array (an array of arrays), it
returns:
Dim strResult()() As String
it does not return a two dimensional array:
Dim strResult(,) As String
Each row of a jagged array can be a different length (it has jagged edges),
each row of a two dimensional array has to be exactly the same length!
http://msdn.microsoft.com/msdnmag/issues/02/02/NET/ http://msdn.microsoft.com/library/de...ringarrays.asp
Third:
SplitFields
takes single Characters as delimiters vbCrLF & "..." will not function as
you expect! As both are strings not single characters! In fact you cannot
pass a string into a Character parameter, with Option Strict On (hence a
easy to fix compile error, rather then obscure runtime error).
Try:
Imports VB = Microsoft.VisualBasic
' Separators a string based on Char delimiters
Public Function SplitFields(ByVal input As String, ByVal separatorLine
As Char, ByVal separatorField As Char) As String()()
Dim lines() As String =
input.Trim(separatorLine).Split(separatorLine)
Dim fields(lines.Length - 1)() As String
For index As Integer = 0 To lines.Length - 1
fields(index) = lines(index).Split(separatorField)
Next
Return fields
End Function
' Separators a string based on String delimiters
Public Function SplitFields(ByVal input As String, ByVal separatorLine
As String, ByVal separatorField As String) As String()()
' TODO: Decide if leading & trailing delimiters need to be
trimmed...
' Could possible use input.Trim(seperatorLine.ToCharArray())
' however that may clean up too much...
Dim lines() As String = VB.Split(input), separatorLine)
Dim fields(lines.Length - 1)() As String
For index As Integer = 0 To lines.Length - 1
fields(index) = VB.Split(lines(index), separatorField)
Next
Return fields
End Function
'strResult = SplitFields(TextBox1.Text, ControlChars.Cr, "."c)
strResult = SplitFields(TextBox1.Text, vbCrLf, "...")
For i = 0 To strResult.Length
For j = 0 to strResult(i).Length
TextBox2.Text = TextBox2.Text & strResult(i)( j) & vbCrLf
Next
Next
With effort you should be able to modify SplitFields to return a two
dimensional array instead of a ragged array.
Hope this helps
Jay
"Ben" <Be*@NoSpam.com> wrote in message
news:uB*************@TK2MSFTNGP15.phx.gbl...
Thanks Jay
I now have this code in a sub calling the function below it.
I have two problems:
Firstly with row 4 i get the error "Number of indices exceeds the number
of dimensions of the indexed array".
Secondly if i continue, but ignoring the above,the when the function
returns it does not seem to get to row 2, but does not error.
Thanks
B
Dim strResult()() As String
Dim i As Integer
Dim j As Integer
1 strResult = SplitFields(TextBox1.Text, vbCrLf, "...")
2 For i = 0 To UBound(strResult(0))
3 For j = 0 To UBound(strResult(1))
4 TextBox2.Text = TextBox2.Text & strResult(i, j) & vbCrLf
5 Next
6 Next
7 Public Function SplitFields(ByVal input As String, ByVal separatorLine
As String, ByVal separatorField As String) As String()()
8 Dim lines() As String = input.Trim(separatorLine).Split(separatorLine)
9 Dim fields(lines.Length - 1)() As String
10 For index As Integer = 0 To lines.Length - 1
11 fields(index) = lines(index).Split(separatorField)
12 Next
13 Return fields
14 End Function
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eN**************@TK2MSFTNGP15.phx.gbl... Ben,
I normally use 2 String.Split calls, one to get the list of lines, and
one in a loop to get the fields in a line.
Something like:
Public Function SplitFields(ByVal input As String, ByVal separatorLine
As Char, ByVal separatorField As Char) As String()()
Dim lines() As String =
input.Trim(separatorLine).Split(separatorLine)
Dim fields(lines.Length - 1)() As String
For index As Integer = 0 To lines.Length - 1
fields(index) = lines(index).Split(separatorField)
Next
Return fields
End Function
Public Sub Main()
Dim input As String = "field1,field2,field3" & ControlChars.Cr _
& "field4,field5,field6" & ControlChars.Cr _
& "field7,field8,field9" & ControlChars.Cr _
& "field10,field11,field12" & ControlChars.Cr
Dim fields()() As String = SplitFields(input, ControlChars.Cr,
","c)
End Sub
Remember that String.Split splits based on individual characters, not the
entire string. If you want to split based on ControlChars.CrLf then you
will want to use Strings.Split instead. Or possibly RegEx.Split
Hope this helps
Jay
"Ben" <Be*@NoSpam.com> wrote in message
news:O6*************@TK2MSFTNGP15.phx.gbl... Hi
I am creating a dynamic function to return a two dimensional array from
a delimeted string.
The delimited string is like:
field1...field2...field3...
field1...field2...field3...
field1...field2...field3...
field1...field2...field3...
I have this code which errors on the two dimentional array, but I am
also wondering if there is a one step method of performing the job
Any advice would be much appreciated
Thanks
B
Dim strRows() As String
Dim strResult(0, 0) As String
Dim i As Integer
strRows = Split(strDelim, vbCrLf)
For i = 0 To UBound(strRows)
If Len(strRows(i)) > 0 Then
strResult(i, UBound(strResult()) = Split(strRows(i), "..."))
End If
Next i