473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

whats the best way to do this?

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
19 2094
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
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
"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
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
"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
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
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
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
"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
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
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
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
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
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
"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
"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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eva | last post by:
Hi all, I just wanted some advise on what control is best to use in my situaton. What i am trying to achieve is to allow the user to enter 1 or more room names into a control of some sort so...
3
by: Kevin Steffer | last post by:
Hi group I have a webform which I want to make an ftp connection for a filetransfer from. The thing is when I use the WebRequest class it says "The URI prefix is not recognized" and my URI is...
2
by: SOR | last post by:
I'm writing a guestbook and given the number of entrys the guestbook might have can vary quite a lot - the nav links to view the guestbook entrys need to generated live at the time to suit . ...
5
by: Panama Red | last post by:
I would like to learn to program in c++ on Linux and AIX systems...mainly socket and fifo type stuff. Can someone recommend a book for someone with experience only with Perl, shell, and Pick/Basic...
4
by: David Lozzi | last post by:
OK simple question. Whats the default value for an string() array? sub LoadStuff(byval one as integer, byval two as string, optional byval three() as string = ??) Its driving me nuts! ...
4
by: markrush | last post by:
if i have 2 datasources with different table names and column headers that i want to merge i.e. "ptitle" and "name" whats the best way of doing this? are there any standard routines or should i use...
16
by: Brian Henry | last post by:
Is there a listing out there anywhere that lists what is new in .NET 2.0 mainly in VB? I've seen simple lists like oh we have all these new controls, but I want a class list and such also. thanks!
2
by: moondaddy | last post by:
I'm using WPF and c#. Whats the best way for a child class to know about it's parent class? For examle class ParentClass : CollectionBase { // code... class ChildClass { // code...
4
by: LoneHunter01 | last post by:
Basically, I just need a general direction on where to go for this. Yes, this is for a school project, though it's strictly an optional one (and I have tried many solutions, one in-depth). We've...
7
by: Paulo | last post by:
Hi, what is diference between: File -New Web Site and File -New Project -VB/C# -Web Application ?????? VS 2005
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.