一个string类型的字符串例如:"1,1,1,2,2"和"1,1,2,3,3,3"转换成一个数组这个数组是由相同数字(字符)的长度的值组成,例如上两个字符串分别应转换成数组{3,2}和{2,1,3}

解决方案 »

  1.   

    其实这个并不太适合用正则来做,最近忙,就只给出正则实现方法吧
    string test = "1,1,2,3,3,3";
    List<int> list = new List<int>();
    MatchCollection mc = Regex.Matches(test, @"(\d+)(,\1)*");
    foreach (Match m in mc)
    {
        list.Add(m.Value.Length - m.Value.Replace(",", "").Length + 1);
    }int[] result = list.ToArray();
      

  2.   

    public void GetNumberArrayFromString()
    {
                string[] resultString = Regex.Split("1,1,1,2,2", ",", RegexOptions.None);
                int[] numberArray = new int[resultString.Length];
                int i = 0;
                foreach (string numberstring in resultString)
                {
                    numberArray[i] = Convert.ToInt32(numberstring);
                    i++;            
                }
    }//得到整型数组{1,1,1,2,2}再循环进行相同累计就行了
      

  3.   

    List<int> list = new List<int>()无效我用的是.net1.1,不是2.0
      

  4.   

    那就用ArrayList,不过转数组就要自己来转了,没有现成的方法string test = "1,1,2,3,3,3";
    ArrayList list = new ArrayList();
    MatchCollection mc = Regex.Matches(test, @"(\d+)(,\1)*");
    foreach (Match m in mc)
    {
        list.Add(m.Value.Length - m.Value.Replace(",", "").Length + 1);
    }
      

  5.   

    string str = "1,1,1,2,2,3,4,4,5";
    string[] ss = str.Split(',');
    int cont = 1;
    ArrayList arr = new ArrayList();
    for(int i=0;i<ss.Length;i++)
    {
    try
    {
    if(ss[i]==ss[i+1]) cont ++;
    else{ arr.Add(cont);cont=1; }
    }
    catch{ arr.Add(cont); }
    }