日期:2014-05-19  浏览次数:20791 次

拆分数组问题
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完成,但现在是 "|| "
有谁遇到这样的问题



------解决方案--------------------
andy 是 "1634||工商分局;1684||广东工商局; ",还是 "1634||工商分局 "这样的

string andy = "1634||工商分局 ";
string[] state = Regex.Split(andy , @ "\|\| ");
string andylu = state[0];
string andylu1 = state[1];

------解决方案--------------------
不用正则表达式那么麻烦,直接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();
// 第二次拆分“||”