您的位置:程序门 -> .net技术 -> c#



拆分数组问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


拆分数组问题[已结贴,结贴人:mengyao]
发表于:2007-04-03 21:10:13 楼主
string   msitename   =   textsarcher.text.trim();
                msxml2.xmlhttp   xmlhttp   =   new   msxml2.xmlhttp();
                string   url   =   "http://xxx.xx.x.xx/getservice.php?sitename= "   +   msitename   +   " ";

                xmlhttp.open( "get ",   url,   false,   null,   null);
                xmlhttp.send( " ");
                msxml2.xmldocument   dom   =   new   xmldocument();
                byte[]   b   =   (byte[])xmlhttp.responsebody;
               
                //utf8格式
                //string   flag   =   system.text.asciiencoding.utf8.getstring(b,   0,   b.length);
               
                string   andy   =   system.text.encoding.getencoding( "gb2312 ").getstring(b).trim();

                //response.write(url);
                //response.write(flag);
                response.write(andy);
                //response.end();
                string[]   state   =   andy.split(new   char[]   {   ' ¦ '   });
                string   andylu   =   state[0];
                string   andylu1   =   state[1];

                response.write(andylu);
                response.write(andylu1);


返回数组格式是:
1634 ¦ ¦工商分局;1684 ¦ ¦广东工商局;
现在问题是怎么样拆分,我用split完成,但现在是 " ¦ ¦ "
有谁遇到这样的问题

发表于:2007-04-03 22:19:311楼 得分:0
mark
发表于:2007-04-03 22:37:482楼 得分:5
andy   是 "1634 ¦ ¦工商分局;1684 ¦ ¦广东工商局; ",还是 "1634 ¦ ¦工商分局 "这样的

string   andy   =   "1634 ¦ ¦工商分局 ";
string[]   state   =   regex.split(andy   ,   @ "\ ¦\ ¦ ");
string   andylu   =   state[0];
string   andylu1   =   state[1];
发表于:2007-04-03 22:48:003楼 得分:0
3366 ¦ ¦xx电信绿化西路营业厅;1836 ¦ ¦xx电信分局;1835 ¦ ¦xx电信;……
nuber1 ¦ ¦name1;number2 ¦ ¦name2;……nubern ¦ ¦namen
这个已经 " ¦ ¦ "解决
string[]   state   =   system.text.regularexpressions.regex.split(andy,   @ "\ ¦\ ¦ ");
现在因为有第一次要折分“;”
第二次拆分“ ¦ ¦”
有个问题,当把这个“;”拆分之后,就成了
3366 ¦ ¦xx电信绿化西路营业厅1836 ¦ ¦xx电信分局1835 ¦ ¦xx电信……
这们更难操作?
发表于:2007-04-03 23:05:594楼 得分:0
try

string   andy   =   "3366 ¦ ¦xx电信绿化西路营业厅;1836 ¦ ¦xx电信分局;1835 ¦ ¦xx电信; ";
matchcollection   mc   =   regex.matches(andy,   @ "([^\ ¦]+)(?=\ ¦ ¦ ¦$) ");
foreach   (match   m   in   mc)
{
        richtextbox2.text   +=   m.value   +   "\n ";
}
发表于:2007-04-03 23:15:105楼 得分:0
楼上的msn是?
我好好想向您请教书?
发表于:2007-04-03 23:17:166楼 得分:5
不用正则表达式那么麻烦,直接split就可以了,如下代码所示(建议直接贴到visual   studio里面运行了看):

using   system;
using   system.collections.generic;
using   system.text;

namespace   split
{
        class   program
        {
                struct   andy
                {
                        public   int   id;
                        public   string   name;
                }

                static   void   main(string[]   args)
                {
                        string   s   =   "3366 ¦ ¦xx电信绿化西路营业厅;1836 ¦ ¦xx电信分局;1835 ¦ ¦xx电信; ";

                        //   第一次拆分“;”
                        string[]   andyarray   =   s.split(new   char[]   {   '; '   },   stringsplitoptions.removeemptyentries);
                        andy[]   andies   =   new   andy[andyarray.length];
                        for   (int   i   =   0;   i   <   andyarray.length;   ++i)
                        {
                                andies[i]   =   new   andy();
                                //   第二次拆分“ ¦ ¦”
                                string[]   andyfields   =   andyarray[i].split(new   string[]   {   " ¦ ¦ "   },   stringsplitoptions.removeemptyentries);
                                andies[i].id   =   int.parse(andyfields[0]);
                                andies[i].name   =   andyfields[1];
                        }

                        foreach   (andy   andy   in   andies)
                        {
                                console.writeline( "andy:   id={0},   name={1} ",   andy.id,   andy.name);
                        }
                }
        }
}
发表于:2007-04-03 23:22:457楼 得分:5
如果一定想用正则表达式,如此比较简洁好用些,能够将id和name分清楚:

                        string   andy   =   "3366 ¦ ¦xx电信绿化西路营业厅;1836 ¦ ¦xx电信分局;1835 ¦ ¦xx电信; ";
                        matchcollection   mc   =   regex.matches(andy,   @ "(? <id> \d+)\ ¦\ ¦(? <name> [^ ¦$]+); ");
                        foreach   (match   m   in   mc)
                        {
                                console.writeline( "andy:   id={0},   name={1} ",   m.groups[ "id "],   m.groups[ "name "]);
                        }
发表于:2007-04-04 08:18:168楼 得分:0
@ "([^\ ¦]+)(?=\ ¦ ¦ ¦$) ");代表什么意思
发表于:2007-04-04 13:36:489楼 得分:0
tonywhite(白白):
你这不是在web下的吧?
发表于:2007-04-04 14:37:2810楼 得分:0
先用;分割,在对起得到的数组进行 ¦ ¦分割.
发表于:2007-04-05 00:56:5311楼 得分:5
mengyao(edongguan)   :
您写的asp.net吧。我给的那段code无论是在webapplication下还是windowsapplication,都是可用的,因为用的都是.net基本类库。

duanzhi1984(莫邪):
这段正则表达式意思是除了“ ¦”、“;”之外其它字符的一个或多个,紧跟一个“ ¦”或“;”或行结束符。关于正则表达式的具体解释请查阅msdn   regular   expression章节。
发表于:2007-04-05 00:58:2312楼 得分:0
mengyao(edongguan)   :
我指的是后一段code。前一段code把main函数里的内容和andy类的定义抄下来拷贝到web   application里头就能用了。
发表于:2007-04-09 20:33:4613楼 得分:0
楼上我还是要不耻下问?
发表于:2007-04-09 20:39:3814楼 得分:0
regular   expression

如果格式一定都是以 ¦ ¦分割   用split函数也很好
发表于:2007-04-15 11:05:4115楼 得分:0
我现在分割后想写入数据表中


快速检索

最新资讯
热门点击