当前位置:编程学习 > C#/ASP.NET >>

格式化的字符串如何转化成数组或ArrayList

需要转换的字符串为:
PVG,SHA@HKG,GHK@BKK或PVG,SHA,SHB@HKG,GHK@BKK@CKK,PKK 。外层用@符号分隔,内层英文逗号分隔,分隔了多少数据不固定。

最终得到的结果为:
Array[0]="PVG-HKG-BKK"
Array[1]="PVG-GHK-BKK"
Array[2]="SHA-HKG-BKK"
Array[3]="SHA-GHK-BKK"

具体怎么实现?小弟急用这个算法,希望高手赐教!


--------------------编程问答-------------------- 分割完了,还要排序? --------------------编程问答-------------------- 不要沉了,求解!! --------------------编程问答--------------------
string[] fields = "PVG,SHA@HKG,GHK@BKK或PVG,SHA,SHB@HKG,GHK@BKK@CKK,PKK".Split("@".ToCharArray());
--------------------编程问答--------------------
引用 1 楼 ybh37 的回复:
分割完了,还要排序?
不要排序 --------------------编程问答--------------------
引用 3 楼 wuyazhe 的回复:
C# code
string[] fields = "PVG,SHA@HKG,GHK@BKK或PVG,SHA,SHB@HKG,GHK@BKK@CKK,PKK".Split("@".ToCharArray());

这样不是我要的哦 --------------------编程问答--------------------
引用 5 楼 zxjyuan 的回复:
引用 3 楼 wuyazhe 的回复:
C# code
string[] fields = "PVG,SHA@HKG,GHK@BKK或PVG,SHA,SHB@HKG,GHK@BKK@CKK,PKK".Split("@".ToCharArray());

这样不是我要的哦

要用到嵌套循环,递归等东东,小弟不才,求高手帮忙 --------------------编程问答-------------------- PVG,SHA,SHB@HKG,GHK@BKK@CKK,PKK

 也想得到这样的结果?:
 
Array[0]="PVG-HKG-BKK"
Array[1]="PVG-GHK-BKK"
Array[2]="SHA-HKG-BKK"
Array[3]="SHA-GHK-BKK" --------------------编程问答--------------------
引用 7 楼 q107770540 的回复:
PVG,SHA,SHB@HKG,GHK@BKK@CKK,PKK

 也想得到这样的结果?:
 
Array[0]="PVG-HKG-BKK"
Array[1]="PVG-GHK-BKK"
Array[2]="SHA-HKG-BKK"
Array[3]="SHA-GHK-BKK"


对不起,是PVG,SHA@HKG,GHK@BKK --------------------编程问答-------------------- 拿分的速度速度 --------------------编程问答-------------------- 你的意思是这样吗?
PVG
SHA@HKG
GHK@BKK

Array[0]="PVG-HKG-BKK"
Array[1]="PVG-GHK-BKK"
Array[2]="SHA-HKG-BKK"
Array[3]="SHA-GHK-BKK"

其实就是数组里放数组吧?

Array[]{new string[]{PVG},new string[]{SHA,HKG},new string[]{GHK,BKK}}

然后循环吧? --------------------编程问答-------------------- do(string s,string[] ss,List<string> list)
{
   string out=s;
  for(ss)
 {
   out+=ss[i];
 }
list.Add(out);
} --------------------编程问答-------------------- 大概思路,我没太看明白你要怎么组合,稍微改变一下就可以了吧

你这就2层不用递归了

如果递归,就把我方法里的string[]改成array就可以了吧,然后判断array里的元素是不是array,是就递归 --------------------编程问答-------------------- string str="PVG,SHA@HKG,GHK@BKK";
string[] arr=str.Split(new string[]{",","@"},StringSplitOptions.RemoveEmptyEntries);
获取数组后实现排列组合
http://topic.csdn.net/u/20090217/21/f41ed9f6-f929-451c-a5c9-80d2e408422a.html --------------------编程问答--------------------
引用 13 楼 wuyq11 的回复:
string str="PVG,SHA@HKG,GHK@BKK";
string[] arr=str.Split(new string[]{",","@"},StringSplitOptions.RemoveEmptyEntries);
获取数组后实现排列组合
http://topic.csdn.net/u/20090217/21/f41ed9f6-f929-451c-a5c9-80d2e……


我也觉得lz的意思是先分拆,然后在组合。

PVG,SHA@HKG,GHK@BKK
分拆成
PVG,SHA -》在分拆成 PVG SHA 两个
HKG,GHK -》在分拆成 HKG GHK 两个
BKK

最后取组合
Array[0]="PVG-HKG-BKK"
Array[1]="PVG-GHK-BKK"
Array[2]="SHA-HKG-BKK"
Array[3]="SHA-GHK-BKK"

分拆应该不难,split即可,关键是后面的三个(不定)数组,需要每组取一个数组合出新的数据.可能需要大段的for循环了,目前还没有想到更好的办法,明天上班在试试。
--------------------编程问答--------------------

 private void A001()
        {
            //PVG,SHA@HKG,GHK@BKK或PVG,SHA,SHB@HKG,GHK@BKK@CKK,PKK
            //String s1 = "PVG,SHA@HKG,GHK@BKK";
            String s1 = "PVG,SHA,SHB@HKG,GHK@BKK@CKK,PKK";


            IList<String[]> strRtn = new List<String[]>();
            String[] strA = s1.Split('@');
            
            foreach (String stmp1 in strA)
            {
                String[] strB = stmp1.Split(',');
                strRtn.Add(strB);
            }


            IList<String> ilNew = new List<String>();
            for (int i = 0; i < strRtn.Count; i ++ )
            {
                 GetH(strRtn, i ,ref ilNew);
            }


            String[] strMsg = ilNew.ToArray<String>();
            MessageBox.Show(String.Join("|", strMsg));

        }

        private void GetH(IList<String[]> strRtn , int iparm ,ref IList<String> strParm)
        {
            int iCount = strParm.Count;

            if (iCount > 0)
            {
                for (int i = 1; i < strRtn[iparm].Count<String>() ; i++)
                {
                    for (int j = 0; j < iCount; j++)
                    {
                        String str2 = strParm[j];
                        strParm.Add(str2);
                    }
                }

                for (int j = 0; j < strRtn[iparm].Count<String>(); j++)
                {
                    for (int i = j * iCount; i < iCount * (j + 1); i++)
                    {
                        strParm[i] = strParm[i] + "-" + strRtn[iparm][j];
                    }
                }
            }
            else
            {
                for (int j = 0; j < strRtn[iparm].Count<String>(); j++)
                {
                    strParm.Add(strRtn[iparm][j]);
                }
            }
        }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,