473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Split Delimited Text Twice into Array

Ben
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
Nov 21 '05 #1
3 9635
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

Nov 21 '05 #2
Ben
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


Nov 21 '05 #3
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



Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nieuws | last post by:
Hi, I was trying to do the following. It's my first php "project", so it's quiet logic that i have some problems. Perhaps the php community might help. It's about this : I have a txt file...
5
by: chrisgeary | last post by:
i have a function (below) which reads the last n lines from a text file. rather than read the whole line and output it as is, i want to be able to read the line and split the tab delimited text...
7
by: Christine | last post by:
My code has a split function that should split the text file of numbers. I've run this in previous programs as it is here and it worked, but now it wont work for some reason and returns...
3
by: Jan Hanssen | last post by:
Hi! I have a list of data in a textfile which is tab delimited. Each line is seperated by a VbCrLf. I want to collect this data in a multidimensional string array. I do not wish to use a...
4
by: Craig Buchanan | last post by:
I am trying to split a comma-delimited string into a string array. unfortunately, if the string doesn't contain a comma, the resulting array is Nothing. other than using vb6 compatibility, is...
3
by: Microsoft | last post by:
I have a multine list that I would like to split into an array. I paste it into a richtext box and go from there, but it just makes the first part of the array the whole list with little boxes...
2
by: Jeff | last post by:
Hello all! I created a successful program that reads data from a reliable tab-delimited file - or so I thought. After getting everything to work with small files, I changed the input to a larger...
1
by: clayalphonso | last post by:
Here is the code: <% dim testArray, testArray2 dim Conn, rs dim sSQL, sConnString 'response.write request.form("sel1") 'testArray = split(request.form("sel1"),",") 'for each gidstuff In...
2
by: AMP | last post by:
Hello, I am trying to split an Array into another array. Each value in the array has tab delimited strings. I am getting: Cannot implicitly convert type 'string' to 'string' I am trying...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.