什么是最好的方法呢? [英] whats the best way to do this?

查看:79
本文介绍了什么是最好的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建2个字符串然后解析一个字符串以将数据

保存到数据库中。


我的第一个字符串如下所示:


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


然后我需要解析那个字符串,用逗号分隔它们所以我看到

这个

Jo***************@yahoo.com

Gr ******* ****@aol.com

Ke************@gmail.com

然后我需要解析那个字符串所以我得到了

John

Greg

Kelly


史密斯

亨利

史密斯


所以我可以将每个值保存到数据库中,最好的方法是什么?

我调用的存储过程已经用逗号解析了字符串,所以

更容易解析代码中的大字符串并传递

更小的字符串使用管道(|)并对proc进行更改以解析
(b)的
值或者我应该在C#代码中执行所有这些操作?如果用C#方式,

怎么办呢?我能够得到这种字符串格式

(Jo***************@yahoo.com)但是当我尝试解析它时,我''得到错误

(索引超出了数组的范围)

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)

推荐答案

史蒂夫,


SJ **************** @ yahoo.com,Gr *********** @ aol.com ,凯莉|史密斯| ks @gmai


< string> .Split(< separator>)方法可能会有所帮助。
http://msdn2.microsoft.com/en-us/lib ... ing.split.aspx


问候,Alex Meleta

[TechBlog] http://devkids.blogspot.com
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


我目前正在这样做,但当我拆分时第一个字符串由逗号(,)

罚款,它的第二个字符串Jo***************@yahoo.com这给了我

a适合。


当我这样做时:


String [] names = ViewState [" custnames"]。ToString()。Split(''|'');

foreach(名字中的字符串n)

{

字符串[] u = n.ToString()。分割(''|'');

Response.Write(u [0]);

Response.Write(u [1]);

}

我收到此错误:

索引超出了数组的范围,但如果我这样做

String [] names = ViewState [ " custnames"]。ToString()。Split(''|'');

foreach(名字中的字符串n)

{

String [] u = n.ToString()。分割(''|'');

Response.Write(u [0]);

}


我可以看到字符串中的第一个名字没有问题。所以我在sytnax中错过了什么或者做错了什么?


" Alex Meleta" < am ***** @ gmail.comwrote in message

news:15 ************************* *@msnews.microsoft .com ...
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...

嗨Steve,


SJo ******** ******* @ yahoo.com,Gr *********** @ aol.com,凯莉|史密斯| ks @gmai


< ; string> .Split(< separator>)方法可能会有所帮助。
http://msdn2.microsoft.com/en-us/lib...ing.split.aspx


问候,Alex Meleta

[TechBlog] http://devkids.blogspot.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



" Steve" < St *** @ community.nospam.com写信息

新闻:O7 ************** @ TK2MSFTNGP02.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 (字符串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

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


这篇关于什么是最好的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆