473,735 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(strResul t()) = Split(strRows(i ), "..."))

End If

Next i
Nov 21 '05 #1
3 9669
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(ByV al input As String, ByVal separatorLine
As Char, ByVal separatorField As Char) As String()()
Dim lines() As String =
input.Trim(sepa ratorLine).Spli t(separatorLine )
Dim fields(lines.Le ngth - 1)() As String

For index As Integer = 0 To lines.Length - 1
fields(index) = lines(index).Sp lit(separatorFi eld)
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,field1 1,field12" & ControlChars.Cr

Dim fields()() As String = SplitFields(inp ut, 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.Cr Lf 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******** *****@TK2MSFTNG P15.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(strResul t()) = 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(Tex tBox1.Text, vbCrLf, "...")

2 For i = 0 To UBound(strResul t(0))

3 For j = 0 To UBound(strResul t(1))

4 TextBox2.Text = TextBox2.Text & strResult(i, j) & vbCrLf

5 Next

6 Next

7 Public Function SplitFields(ByV al input As String, ByVal separatorLine
As String, ByVal separatorField As String) As String()()

8 Dim lines() As String = input.Trim(sepa ratorLine).Spli t(separatorLine )

9 Dim fields(lines.Le ngth - 1)() As String

10 For index As Integer = 0 To lines.Length - 1

11 fields(index) = lines(index).Sp lit(separatorFi eld)

12 Next

13 Return fields

14 End Function
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:eN******** ******@TK2MSFTN GP15.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(ByV al input As String, ByVal separatorLine
As Char, ByVal separatorField As Char) As String()()
Dim lines() As String =
input.Trim(sepa ratorLine).Spli t(separatorLine )
Dim fields(lines.Le ngth - 1)() As String

For index As Integer = 0 To lines.Length - 1
fields(index) = lines(index).Sp lit(separatorFi eld)
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,field1 1,field12" & ControlChars.Cr

Dim fields()() As String = SplitFields(inp ut, 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.Cr Lf 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******** *****@TK2MSFTNG P15.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(strResul t()) = 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.Visua lBasic

' Separators a string based on Char delimiters
Public Function SplitFields(ByV al input As String, ByVal separatorLine
As Char, ByVal separatorField As Char) As String()()
Dim lines() As String =
input.Trim(sepa ratorLine).Spli t(separatorLine )
Dim fields(lines.Le ngth - 1)() As String

For index As Integer = 0 To lines.Length - 1
fields(index) = lines(index).Sp lit(separatorFi eld)
Next
Return fields
End Function

' Separators a string based on String delimiters
Public Function SplitFields(ByV al 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(sepe ratorLine.ToCha rArray())
' however that may clean up too much...
Dim lines() As String = VB.Split(input) , separatorLine)
Dim fields(lines.Le ngth - 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(Tex tBox1.Text, ControlChars.Cr , "."c)
strResult = SplitFields(Tex tBox1.Text, vbCrLf, "...")
For i = 0 To strResult.Lengt h
For j = 0 to strResult(i).Le ngth
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******** *****@TK2MSFTNG P15.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(Tex tBox1.Text, vbCrLf, "...")

2 For i = 0 To UBound(strResul t(0))

3 For j = 0 To UBound(strResul t(1))

4 TextBox2.Text = TextBox2.Text & strResult(i, j) & vbCrLf

5 Next

6 Next

7 Public Function SplitFields(ByV al input As String, ByVal separatorLine
As String, ByVal separatorField As String) As String()()

8 Dim lines() As String = input.Trim(sepa ratorLine).Spli t(separatorLine )

9 Dim fields(lines.Le ngth - 1)() As String

10 For index As Integer = 0 To lines.Length - 1

11 fields(index) = lines(index).Sp lit(separatorFi eld)

12 Next

13 Return fields

14 End Function
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:eN******** ******@TK2MSFTN GP15.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(ByV al input As String, ByVal separatorLine
As Char, ByVal separatorField As Char) As String()()
Dim lines() As String =
input.Trim(sepa ratorLine).Spli t(separatorLine )
Dim fields(lines.Le ngth - 1)() As String

For index As Integer = 0 To lines.Length - 1
fields(index) = lines(index).Sp lit(separatorFi eld)
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,field1 1,field12" & ControlChars.Cr

Dim fields()() As String = SplitFields(inp ut, 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.Cr Lf 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******** *****@TK2MSFTNG P15.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(strResul t()) = 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
4899
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 with the following data : 1. Stijn Piot 58.12; 2. Kim Van Rooy 1.25; 3. Johnny Marcovich 2.37; 4. John Terlaeken (Bel) 1 ronde/tour; 5. Michael Bertrand 2.12;
5
6196
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 file so I can present it in columns, exclude unwanted data etc. can anyone help me rewrite the javascript function to achieve this? thx Chris
7
4471
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 System_String. I don't see a difference in the previous code and what I have now. Can anyone find where I went wrong? Thanks a bunch! //Open new stream reader and writer to read in file //And write to a new file. StreamWriter swa=new...
3
3234
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 database or a Disconnected ADO Recordset since this is just passing data along to be written into an XML file... I've figured out how to split the data line by line by doing a
4
1325
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 there another option? thanks, Craig Buchanan
3
2515
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 inbetween. Here is the code Dim userdata As String Dim serverarray As String() Dim servercount As Integer
2
1814
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 file that was created by the same program with the same format so hopefully there shouldnt be any problem. Unfortunately after the change, my program could no longer read in data from the file. The split only populates the first element of the...
1
4828
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 testArray 'Response.Write gidstuff & "<br />"
2
2181
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 this: string lineSeparators = new String{"\r\n"}; string Channel =
0
9330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9255
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9202
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8202
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6050
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4567
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3278
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2191
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.