473,239 Members | 1,761 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,239 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 9622
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.