• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

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!
0
pvg1975
Asked:
pvg1975
  • 8
  • 4
  • 4
  • +2
1 Solution
 
Bill NolanCommented:
Some random suggestions:

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"};
0
 
Bill NolanCommented:
Be aware that in (d) above you could face a precedence issue.
0
 
pvg1975Author Commented:
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!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mgfranzCommented:
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>")
0
 
pvg1975Author Commented:
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.IndexOutOfRangeException: Index was outside the bounds of the array.

at

Response.Write(individual1 & individual2 & "<br>")
 

0
 
daz84Commented:
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
0
 
Bill NolanCommented:
What about 'individual'?  Does it contain the two strings you expect?
0
 
pvg1975Author Commented:
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....
0
 
pvg1975Author Commented:
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



0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Please try this:
        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

Open in new window


It works? if yes, then your textbox F4 is empty.
0
 
pvg1975Author Commented:
That works, but F4.text is not empty.... I get a full line at ITEM and I get the name at individual(0)

0
 
pvg1975Author Commented:
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
0
 
mgfranzCommented:
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"
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Ok then try:
                my_array = Split(F4.Text.Trim(), 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

Open in new window

0
 
mgfranzCommented:
Sorry, test individual2 variable for null or empty.
0
 
Bill NolanCommented:
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.)?)
0
 
pvg1975Author Commented:
Wow, that worked.... everything for a TRIM?!?!

Thanks!
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
That it's, you had a new line at the end of your textbox content, the Trim function has removed it.
0
 
pvg1975Author Commented:
Thank you all guys for your comments :)

Paula
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 8
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now