By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

whats the best way to do this?

P: n/a
I have to create 2 strings and then parse one string out to save the data
into the database.

My first string looks like this:

Jo***************@yahoo.com,Gr***********@aol.com, Ke************@gmail.com,

I then need to parse out that string to seperate them by the commas so I see
this
Jo***************@yahoo.com
Gr***********@aol.com
Ke************@gmail.com
I then need to parse out that string so I get
John
Greg
Kelly

Smith
Henry
Smith

so I can save each value to the database, what is the best way to do this?
The stored procedure I'm calling is already parsing strings by the comma, so
would it be easier to just parse out the large string in code and pass the
smaller string with the pipe (|) and make a change to the proc to parse the
values by the (|) or should I do all of this in my C# code? If the C# way,
how would that be done? I'm able to get this string format
(Jo***************@yahoo.com) but when I try to parse it out, I' get errors
(Index was outside the bounds of the array)

Jul 12 '07 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Hi Steve,

SJ****************@yahoo.com,Gr***********@aol.com ,Kelly|Smith|ks@gmai

The <string>.Split(<separator>) method can be helpful.
http://msdn2.microsoft.com/en-us/lib...ing.split.aspx

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
Jul 12 '07 #2

P: n/a
I am currently doing that but when I split the first string by the comma(,)
its fine, its the second string Jo***************@yahoo.com thats giving me
a fit.

when i do this:

String[] names= ViewState["custnames"].ToString().Split('|');
foreach (String n in names)
{
String[] u = n.ToString().Split('|');
Response.Write(u[0]);
Response.Write(u[1]);
}

I'm getting this error:
Index was outside the bounds of the array, but if I do this
String[] names= ViewState["custnames"].ToString().Split('|');
foreach (String n in names)
{
String[] u = n.ToString().Split('|');
Response.Write(u[0]);
}

I can see the first name in the string with no problem. So am I missing
something or doing something wrong in the sytnax?


"Alex Meleta" <am*****@gmail.comwrote in message
news:15**************************@msnews.microsoft .com...
Hi Steve,

SJo***************@yahoo.com,Gr***********@aol.com ,Kelly|Smith|ks@gmai

The <string>.Split(<separator>) method can be helpful.
http://msdn2.microsoft.com/en-us/lib...ing.split.aspx

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com


Jul 12 '07 #3

P: n/a
"Steve" <St***@community.nospam.comwrote in message
news:O7**************@TK2MSFTNGP02.phx.gbl...
so I can save each value to the database, what is the best way to do this?
strRaw =
"Jo***************@yahoo.com,Gr***********@aol.com ,Ke************@gmail.com,";

foreach (string strRecord in strRaw.Split(','))
{
string[] astrElements = strRecord.Split('|');

strFirstName = astrElements[0];
strLastName = astrElements[1];
strEmailAddress = astrElements[2];
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 12 '07 #4

P: n/a
Mark,
I've tried that, I've tried everything I can think of and it only shows
[0] and anything beyond that give me the out of bounds error message.
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:e8**************@TK2MSFTNGP04.phx.gbl...
"Steve" <St***@community.nospam.comwrote in message
news:O7**************@TK2MSFTNGP02.phx.gbl...
>so I can save each value to the database, what is the best way to do
this?

strRaw =
"Jo***************@yahoo.com,Gr***********@aol.com ,Ke************@gmail.com,";

foreach (string strRecord in strRaw.Split(','))
{
string[] astrElements = strRecord.Split('|');

strFirstName = astrElements[0];
strLastName = astrElements[1];
strEmailAddress = astrElements[2];
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 12 '07 #5

P: n/a
"Steve" <St***@community.nospam.comwrote in message
news:O5**************@TK2MSFTNGP05.phx.gbl...
I've tried that, I've tried everything I can think of and it only shows
[0] and anything beyond that give me the out of bounds error message.
Not sure what's happening, then - I've just tried it, and it works fine for
me.

Can you post your whole code...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 12 '07 #6

P: n/a
Hi Steve,

Probably the exception occurs in that piece of code:
SResponse.Write(u[1]);

You should check the boundaries.
Anyway, you can try to analyse that input string is correct, for doing this
change your code to something like code below:

foreach (String n in names)
{
foreach(String justForTest in n.ToString().Split('|'))
{ Response.WriteLine(justForTest); }
Response.WriteLine("-");
}

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com

SI am currently doing that but when I split the first string by the
Scomma(,) its fine, its the second string Jo***************@yahoo.com
Sthats giving me a fit.
S>
Swhen i do this:
S>
SString[] names= ViewState["custnames"].ToString().Split('|');
Sforeach (String n in names)
S{
SString[] u = n.ToString().Split('|');
SResponse.Write(u[0]);
SResponse.Write(u[1]);
S}
SI'm getting this error:
SIndex was outside the bounds of the array, but if I do this
SString[] names= ViewState["custnames"].ToString().Split('|');
Sforeach (String n in names)
S{
SString[] u = n.ToString().Split('|');
SResponse.Write(u[0]);
S}
SI can see the first name in the string with no problem. So am I
Smissing something or doing something wrong in the sytnax?
S>
S"Alex Meleta" <am*****@gmail.comwrote in message
Snews:15**************************@msnews.microsof t.com...
S>
>Hi Steve,

S>
Jo***************@yahoo.com,Gr***********@aol.com ,Kelly|Smith|ks@gmai

The <string>.Split(<separator>) method can be helpful.
http://msdn2.microsoft.com/en-us/lib...ing.split.aspx

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com

Jul 12 '07 #7

P: n/a
sure:

code:
main string:
Jo***************@yahoo.com,Gr***********@aol.com, Ke************@gmail.com,

String[] users= ViewState["Stuff"].ToString().Split(',');

which returns this:
Jo***************@yahoo.com,

then I loop through that string;
foreach (string s in users)
{
string[] s= strRecord.Split('|');

strFirstName = s[0];
strLastName = s[1];
strEmail = s[2];
}
Response.Write(strFirstName + " " + strLastName + " " +
strEmail);

and I get this error:
Index was outside the bounds of the array.
strLastName = astrElements[1];

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:eb**************@TK2MSFTNGP03.phx.gbl...
"Steve" <St***@community.nospam.comwrote in message
news:O5**************@TK2MSFTNGP05.phx.gbl...
>I've tried that, I've tried everything I can think of and it only shows
[0] and anything beyond that give me the out of bounds error message.

Not sure what's happening, then - I've just tried it, and it works fine
for me.

Can you post your whole code...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 12 '07 #8

P: n/a
I've done that and I can see the string that is being passed

Jo***************@yahoo.com,Gr***********@aol.com, Ke************@gmail.com,

I can see that just fine, hell i can even parse it out by the commas, its
splitting this string:
Jo***************@yahoo.com

thats joking on me

"Alex Meleta" <am*****@gmail.comwrote in message
news:15**************************@msnews.microsoft .com...
Hi Steve,

Probably the exception occurs in that piece of code:
SResponse.Write(u[1]);

You should check the boundaries.
Anyway, you can try to analyse that input string is correct, for doing
this change your code to something like code below:

foreach (String n in names)
{
foreach(String justForTest in n.ToString().Split('|'))
{ Response.WriteLine(justForTest); }
Response.WriteLine("-");
}

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com

SI am currently doing that but when I split the first string by the
Scomma(,) its fine, its the second string Jo***************@yahoo.com
Sthats giving me a fit.
SSwhen i do this:
SSString[] names= ViewState["custnames"].ToString().Split('|');
Sforeach (String n in names)
S{
SString[] u = n.ToString().Split('|');
SResponse.Write(u[0]);
SResponse.Write(u[1]);
S}
SI'm getting this error:
SIndex was outside the bounds of the array, but if I do this
SString[] names= ViewState["custnames"].ToString().Split('|');
Sforeach (String n in names)
S{
SString[] u = n.ToString().Split('|');
SResponse.Write(u[0]);
S}
SI can see the first name in the string with no problem. So am I
Smissing something or doing something wrong in the sytnax?
SS"Alex Meleta" <am*****@gmail.comwrote in message
Snews:15**************************@msnews.microsof t.com...
S>
>>Hi Steve,

S>
Jo***************@yahoo.com,Gr***********@aol.co m,Kelly|Smith|ks@gmai

The <string>.Split(<separator>) method can be helpful.
http://msdn2.microsoft.com/en-us/lib...ing.split.aspx

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com


Jul 12 '07 #9

P: n/a
"Steve" <St***@community.nospam.comwrote in message
news:ep**************@TK2MSFTNGP06.phx.gbl...
sure:

code:
<snip>

Hmm - can't see anything obviously wrong with that...

I'm clutching at straws now, but could you tell me what s.Length returns?
I'm wondering if there's some sort of CultureInfo conflict here which has
caused the Split method to fail to interpret the pipe symbol correctly... If
s.Length returns 1 instead of 3, I think that might be the reason...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 12 '07 #10

P: n/a
I'm only using the one string right now just to parse it out. I'm using this
string only right now.
Jo***************@yahoo.com,"
this returns me 80
foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');
Response.Write(astrElements[0].Length);
}

this returns me 520
foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');
Response.Write(strRecord.Length);
}

this returns me 2
String[] userDetails = "Jo***************@yahoo.com,"
Response.Write(userDetails.Length);
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:Ou**************@TK2MSFTNGP03.phx.gbl...
"Steve" <St***@community.nospam.comwrote in message
news:ep**************@TK2MSFTNGP06.phx.gbl...
>sure:

code:

<snip>

Hmm - can't see anything obviously wrong with that...

I'm clutching at straws now, but could you tell me what s.Length returns?
I'm wondering if there's some sort of CultureInfo conflict here which has
caused the Split method to fail to interpret the pipe symbol correctly...
If s.Length returns 1 instead of 3, I think that might be the reason...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 12 '07 #11

P: n/a
Hi Steve,

SJ****************@yahoo.com,Gr***********@aol.com ,Ke************@gmail.com,
Yes, indeed. Because you have the comma at the end of the string and it is
an empty string. You even can split it by '|' but it will be still empty.
So when you try to access it by indexer an exception will be thrown.

So, you should do these:
1. Set up a boundary checks to be sure that length of the array is enough
for access by indexer
2. Bring you string to appropriate format (use Trim methods for cutting or
such) or check that string can be splitted or was splitted correctly.

Hopefully it helps

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com


S>
Jul 12 '07 #12

P: n/a
Hi, Steve.

For that string, this works for me:

<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(Object obj, EventArgs e)
{
string strRaw = "Jo***************@yahoo.com";
string[] astrElements = strRaw.Split('|');

string strFirstName = astrElements[0];
string strLastName = astrElements[1];
string strEmail = astrElements[2];

lblFirstName.Text = strFirstName;
lblLastName.Text = strLastName;
lblEmail.Text = strEmail;

}
</script>
<html>
<body>
<asp:Label ID="lblFirstName" Runat="server" Text=""></asp:Label><br />
<asp:Label ID="lblLastName" Runat="server" Text=""></asp:Label><br />
<asp:Label ID="lblEmail" Runat="server" Text=""></asp:Label><br />
</body>
</html>
------------

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Steve" <St***@community.nospam.comwrote in message news:%2****************@TK2MSFTNGP02.phx.gbl...
I'm only using the one string right now just to parse it out. I'm using this string only right now.
Jo***************@yahoo.com,"
this returns me 80
foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');
Response.Write(astrElements[0].Length);
}

this returns me 520
foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');
Response.Write(strRecord.Length);
}

this returns me 2
String[] userDetails = "Jo***************@yahoo.com,"
Response.Write(userDetails.Length);
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message news:Ou**************@TK2MSFTNGP03.phx.gbl...
>"Steve" <St***@community.nospam.comwrote in message news:ep**************@TK2MSFTNGP06.phx.gbl...
>>sure:

code:

<snip>

Hmm - can't see anything obviously wrong with that...

I'm clutching at straws now, but could you tell me what s.Length returns? I'm wondering if there's some sort of
CultureInfo conflict here which has caused the Split method to fail to interpret the pipe symbol correctly... If
s.Length returns 1 instead of 3, I think that might be the reason...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net


Jul 12 '07 #13

P: n/a
I do that in this

I'm doing that in this;

string temp =
"Jo***************@yahoo.com,Gr***********@aol.com ,Ke************@gmail.com"
;
string strFirstName = string.Empty;
string strLastName = string.Empty;
string strEmail = string.Empty;

string[] userDetails = temp.Split(',');

foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');

strFirstName = astrElements[0];
strLastName = astrElements[1];
strEmail = astrElements[2];
}
Response.Write(strFirstName + " " + strLastName + " " +
strEmail);

but i'm only getting the last person in the string. Also if I use trim(),
I'm getting errors when i compile
"Alex Meleta" <am*****@gmail.comwrote in message
news:15**************************@msnews.microsoft .com...
Hi Steve,

S>
Jo***************@yahoo.com,Gr***********@aol.com, Ke************@gmail.com,
Yes, indeed. Because you have the comma at the end of the string and it is
an empty string. You even can split it by '|' but it will be still empty.
So when you try to access it by indexer an exception will be thrown.
So, you should do these:
1. Set up a boundary checks to be sure that length of the array is enough
for access by indexer
2. Bring you string to appropriate format (use Trim methods for cutting or
such) or check that string can be splitted or was splitted correctly.

Hopefully it helps

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com


S>

Jul 12 '07 #14

P: n/a
Hi Steve,

Jo***************@yahoo.com,Gr***********@aol.com, Ke************@gmail.com
*,*

See ending comma. It gives you 4 string and the 4th is empty. You can prevent
it by checking (as I mentioned before) but if it's an incorrect format it
may be better to thrown your own exception to certainly define them.
Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
Jul 12 '07 #15

P: n/a
"Steve" <St***@community.nospam.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
I'm only using the one string right now just to parse it out. I'm using
this string only right now.
Jo***************@yahoo.com,"

this returns me 80
That simply isn't possible! There aren't even 80 letters in that string,
never mind 80 elements!

Are you following the method I showed you?

Specifically, you need first to split the string by the comma to give an
array of strings for each record.

Then you need to split each record by the pipe symbol to give an array of
individual elements.

I don't think you're doing that...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 12 '07 #16

P: n/a
"Alex Meleta" <am*****@gmail.comwrote in message
news:15**************************@msnews.microsoft .com...
Jo***************@yahoo.com,Gr***********@aol.com, Ke************@gmail.com
*,*

See ending comma. It gives you 4 string and the 4th is empty. You can
prevent it by checking (as I mentioned before) but if it's an incorrect
format it may be better to thrown your own exception to certainly define
them.
Yes, that's very true.

What I normally do is trim the string by the split character first
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 12 '07 #17

P: n/a
"Steve" <St***@community.nospam.comwrote in message
news:u5**************@TK2MSFTNGP04.phx.gbl...
but i'm only getting the last person in the string.
Hmm - is it possible that each record is overwriting the previous one so
that only the last record remains? I don't think Response.Write would do
that, but we're starting to run out of options here!
Also if I use trim(), I'm getting errors when i compile
You will do, because C# is case-sensitive. Try this instead:
string[] userDetails = temp.Trim(',').Split(',');
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 12 '07 #18

P: n/a
re:
!but i'm only getting the last person in the string. Also if I use trim(),

To get around that, after :

string[] astrElements = strRecord.Split('|');

....iterate through the array :

Response.Write(astrElements[0].ToString() + "<br />" );
Response.Write(astrElements[1].ToString() + "<br />" );
Response.Write(astrElements[2].ToString() + "<br />" );

You can also resplit astrElements[i] and parse the individual records.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Steve" <St***@community.nospam.comwrote in message news:u5**************@TK2MSFTNGP04.phx.gbl...
>I do that in this

I'm doing that in this;

string temp = "Jo***************@yahoo.com,Gr***********@aol.com ,Ke************@gmail.com" ;
string strFirstName = string.Empty;
string strLastName = string.Empty;
string strEmail = string.Empty;

string[] userDetails = temp.Split(',');

foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');

strFirstName = astrElements[0];
strLastName = astrElements[1];
strEmail = astrElements[2];
}
Response.Write(strFirstName + " " + strLastName + " " + strEmail);

but i'm only getting the last person in the string. Also if I use trim(), I'm getting errors when i compile
"Alex Meleta" <am*****@gmail.comwrote in message news:15**************************@msnews.microsoft .com...
>Hi Steve,

SJo***************@yahoo.com,Gr***********@aol.co m,Ke************@gmail.com,
Yes, indeed. Because you have the comma at the end of the string and it is an empty string. You even can split it by
'|' but it will be still empty. So when you try to access it by indexer an exception will be thrown.
So, you should do these:
1. Set up a boundary checks to be sure that length of the array is enough for access by indexer
2. Bring you string to appropriate format (use Trim methods for cutting or such) or check that string can be splitted
or was splitted correctly.

Hopefully it helps

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com


S>


Jul 12 '07 #19

P: n/a
Here's the complete parser example :

<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(Object obj, EventArgs e)
{
string temp = "Jo***************@yahoo.com,Gr***********@aol.com ,Ke************@gmail.com";
string strFirstName = string.Empty;
string strLastName = string.Empty;
string strEmail = string.Empty;

string[] userDetails = temp.Split(',');

foreach (string strRecord in userDetails)
{
string[] astrElements = strRecord.Split('|');

Response.Write(astrElements[0].ToString() + "<br />" );
Response.Write (astrElements[1].ToString() + "<br />" );
Response.Write (astrElements[2].ToString() + "<br />" );
}
}
</script>
<html>
<body>
</body>
</html>

----------------

You can also resplit astrElements[i] on the pipe for the individual FirstName, LastName, Email items.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message news:uI**************@TK2MSFTNGP03.phx.gbl...
"Steve" <St***@community.nospam.comwrote in message news:u5**************@TK2MSFTNGP04.phx.gbl...
>but i'm only getting the last person in the string.

Hmm - is it possible that each record is overwriting the previous one so that only the last record remains? I don't
think Response.Write would do that, but we're starting to run out of options here!
>Also if I use trim(), I'm getting errors when i compile

You will do, because C# is case-sensitive. Try this instead:
string[] userDetails = temp.Trim(',').Split(',');
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 12 '07 #20

This discussion thread is closed

Replies have been disabled for this discussion.