pvg1975
asked on
Split function in ASP.NET
HI all,
Im using this code:
my_array = Split(EnteredText, vbCrLf)
For Each item In my_array
individual = Split(item, ",")
Response.Write(individual( 0) & individual(1) & "<br>")
next
The content of EnteredText is a name, and email with a vbCrLf at the end, something as:
andrew, andrew@gmail.com
felix, felix@hotmail.com
jonny, jonny@yahoo.com
etc....
This content is on a textbox.
I need to split the lines first, that's why I use
my_array = Split(EnteredText, vbCrLf)
For Each item In my_array
and once I have the lines, I need to split the name and email (with the comma), so I use
individual = Split(item, ",")
but its not recognizing the email at individual(1)
I get this error:
Index was outside the bounds of the array.
at
Response.Write(individual( 0) & individual(1) & "<br>")
Any thoughts?
Thanks!
Im using this code:
my_array = Split(EnteredText, vbCrLf)
For Each item In my_array
individual = Split(item, ",")
Response.Write(individual(
next
The content of EnteredText is a name, and email with a vbCrLf at the end, something as:
andrew, andrew@gmail.com
felix, felix@hotmail.com
jonny, jonny@yahoo.com
etc....
This content is on a textbox.
I need to split the lines first, that's why I use
my_array = Split(EnteredText, vbCrLf)
For Each item In my_array
and once I have the lines, I need to split the name and email (with the comma), so I use
individual = Split(item, ",")
but its not recognizing the email at individual(1)
I get this error:
Index was outside the bounds of the array.
at
Response.Write(individual(
Any thoughts?
Thanks!
Be aware that in (d) above you could face a precedence issue.
ASKER
Hi Slim,
a) Data is consistent:
andrew, andrew@gmail.com
felix, felix@hotmail.com
jonny, jonny@yahoo.com
b) My_array is perfect
c) vbCrLf is fine, it actually works perfect on the first split. cbCrLf is defined by the user when hits enter on a textbox
d) I have no clue about the multiple delimiters... I basically have to save the first name and email address on a table...
Thanks!
a) Data is consistent:
andrew, andrew@gmail.com
felix, felix@hotmail.com
jonny, jonny@yahoo.com
b) My_array is perfect
c) vbCrLf is fine, it actually works perfect on the first split. cbCrLf is defined by the user when hits enter on a textbox
d) I have no clue about the multiple delimiters... I basically have to save the first name and email address on a table...
Thanks!
Try this;
Dim individual As String() = Split(item, ",")
Dim individual1 As String = individual(0)
Dim individual2 As String = individual(1)
Response.Write(individual1 & individual2 & "<br>")
Dim individual As String() = Split(item, ",")
Dim individual1 As String = individual(0)
Dim individual2 As String = individual(1)
Response.Write(individual1
ASKER
Hi Mgfranz,
I get the same error:
Index was outside the bounds of the array.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeExce ption: Index was outside the bounds of the array.
at
Response.Write(individual1 & individual2 & "<br>")
I get the same error:
Index was outside the bounds of the array.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeExce
at
Response.Write(individual1
I had a good look at your code and can't see anything obvious, so I popped it into wscript and run it with no errors.
It seems perfectly fine, although I was able to recreate your error but omitting a comma from a line, so to me it looks like an input error. Some basic error handling should be able to deal with that just dandy.
Two ways to approach it, either catch the error and decide what to do with it, or my favourite would be to simply have the inner loop only run if instr(item,",") equals true, otherwise ignore it or respond an appropriate message
It seems perfectly fine, although I was able to recreate your error but omitting a comma from a line, so to me it looks like an input error. Some basic error handling should be able to deal with that just dandy.
Two ways to approach it, either catch the error and decide what to do with it, or my favourite would be to simply have the inner loop only run if instr(item,",") equals true, otherwise ignore it or respond an appropriate message
What about 'individual'? Does it contain the two strings you expect?
ASKER
Hi Daz,
This is the exact text Im entering on the textbox:
andrew, andrew@gmail.com
felix, felix@hotmail.com
jonny, jonny@yahoo.com
Still get the error...
and I checked the content of ITEM and I get three results:
andrew, andrew@gmail.com
felix, felix@hotmail.com
and
jonny, jonny@yahoo.com
Im really lost here....
This is the exact text Im entering on the textbox:
andrew, andrew@gmail.com
felix, felix@hotmail.com
jonny, jonny@yahoo.com
Still get the error...
and I checked the content of ITEM and I get three results:
andrew, andrew@gmail.com
felix, felix@hotmail.com
and
jonny, jonny@yahoo.com
Im really lost here....
ASKER
HEre's my full code:
my_array = Split(F4.Text, vbCrLf)
For Each item In my_array
Dim individual As String() = Split(item, ",")
Dim individual1 As String = individual(0)
Dim individual2 As String = individual(1)
Response.Write(individual( 0) & "<br>")
Response.Write(individual( 1) & "<br>")
next
It dies at
Response.Write(individual( 1) & "<br>")
And the content of F4.text is exactly this one:
andrew, andrew@gmail.com
felix, felix@hotmail.com
jonny, jonny@yahoo.com
my_array = Split(F4.Text, vbCrLf)
For Each item In my_array
Dim individual As String() = Split(item, ",")
Dim individual1 As String = individual(0)
Dim individual2 As String = individual(1)
Response.Write(individual(
Response.Write(individual(
next
It dies at
Response.Write(individual(
And the content of F4.text is exactly this one:
andrew, andrew@gmail.com
felix, felix@hotmail.com
jonny, jonny@yahoo.com
Please try this:
It works? if yes, then your textbox F4 is empty.
Dim myText As String
myText = "andrew, andrew@gmail.com" + vbCrLf +
"felix, felix@hotmail.com" + vbCrLf +
"jonny, jonny@yahoo.com"
my_array = Split(myText, vbCrLf)
For Each item In my_array
Dim individual As String() = Split(item, ",")
Dim individual1 As String = individual(0)
Dim individual2 As String = individual(1)
Response.Write(individual(0) & "<br>")
Response.Write(individual(1) & "<br>")
next
It works? if yes, then your textbox F4 is empty.
ASKER
That works, but F4.text is not empty.... I get a full line at ITEM and I get the name at individual(0)
ASKER
This works perfect:
my_array = Split(F4.Text, vbCrLf)
For Each item In my_array
Response.Write(item & "<br>")
next
and print the three lines.
This dies:
my_array = Split(F4.Text, vbCrLf)
For Each item In my_array
Dim individual As String() = Split(item, ",")
Dim individual1 As String = individual(0)
Dim individual2 As String = individual(1)
Response.Write(individual( 0) & "<br>")
Response.Write(individual( 1) & "<br>")
next
my_array = Split(F4.Text, vbCrLf)
For Each item In my_array
Response.Write(item & "<br>")
next
and print the three lines.
This dies:
my_array = Split(F4.Text, vbCrLf)
For Each item In my_array
Dim individual As String() = Split(item, ",")
Dim individual1 As String = individual(0)
Dim individual2 As String = individual(1)
Response.Write(individual(
Response.Write(individual(
next
No, no, no... Do a response.write on the variable, not the array content;
Response.Write(individual1 & individual2 & "<br>")
It's possible that the contents of individual(1) could be empty, try a If..Then test on the contents;
If individual1= "" Then
Response.Write "contents empty"
Response.Write(individual1
It's possible that the contents of individual(1) could be empty, try a If..Then test on the contents;
If individual1= "" Then
Response.Write "contents empty"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, test individual2 variable for null or empty.
Which item does it fail on? (I am assuming the 3rd, since you've inspected it.) Does it fail if you manually add 'individual(1)' to your watch window?
(Btw, the contents of F4.text you've given are NOT the exact contents - you are showing the contents as displayed by a text viewer. What are the actual characters (including CR/LF, etc.)?)
(Btw, the contents of F4.text you've given are NOT the exact contents - you are showing the contents as displayed by a text viewer. What are the actual characters (including CR/LF, etc.)?)
ASKER
Wow, that worked.... everything for a TRIM?!?!
Thanks!
Thanks!
I think that she has a extra line in the textbox, which she cant see, so try with F4.Text.Trim(), like the next example:
my_array = Split(F4.Text.Trim(), vbCrLf)
For Each item In my_array
Dim individual As String() = Split(item, ",")
Response.Write(individual(0) & "<br>")
Response.Write(individual(1) & "<br>")
Next
That it's, you had a new line at the end of your textbox content, the Trim function has removed it.
ASKER
Thank you all guys for your comments :)
Paula
Paula
a) Check the data (is there an inconsistency).
b) Inspect 'my_array' in the debugger.
c) How exactly have you defined 'vbCrLf'?
d) You can use multiple delimiters, such as:
Split(new string[] { "\r\n", "\n"};