Link to home
Start Free TrialLog in
Avatar of pvg1975
pvg1975Flag for Argentina

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!
Avatar of Bill Nolan
Bill Nolan
Flag of United States of America image

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"};
Be aware that in (d) above you could face a precedence issue.
Avatar of pvg1975

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!
Avatar of Mark Franz
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>")
Avatar of pvg1975

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.IndexOutOfRangeException: Index was outside the bounds of the array.

at

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

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
What about 'individual'?  Does it contain the two strings you expect?
Avatar of pvg1975

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....
Avatar of pvg1975

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



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.
Avatar of pvg1975

ASKER

That works, but F4.text is not empty.... I get a full line at ITEM and I get the name at individual(0)

Avatar of pvg1975

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
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"
ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.)?)
Avatar of pvg1975

ASKER

Wow, that worked.... everything for a TRIM?!?!

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

Open in new window

That it's, you had a new line at the end of your textbox content, the Trim function has removed it.
Avatar of pvg1975

ASKER

Thank you all guys for your comments :)

Paula