string str = "df,122,12e,12g,12e,12d2,12d2,12d2,";
每三个字符分成一个新数组

解决方案 »

  1.   

    循环一下长度就可以咯感觉楼主是按逗号分吧?可以用string.Split(',')
      

  2.   

    split如何把字符串从第三个处分开???
      

  3.   

    先用string.split(','); 将一个个的字符串放到数组中 
    然后再用for循环 每三个一组 记得要加上 ","
      

  4.   

    string.replace(",",""); //替换掉逗号
    再用for循环    可以减小空间复杂度(去掉了一个临时的数组)^_^
      

  5.   


    string str = "df,122,12e,12g,12e,12d2,12d2,12d2,"; 
    str.Replace(",", "");str字符串的值为:"df12212e12g12e12d212d212d2"
    通过for循环将str字符串3个字符为一组进行分组。
      

  6.   

      string str = "";
                int i = 0;
                Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>();
                List<String> lst = new List<string>();
                string[] sp = str.Split(new char[1] { ',' });
                foreach (string s in sp)
                {
                    if (i % 3 == 0)
                    {
                        dic.Add(i, lst);
                        lst.Clear();
                    }
                    else
                        lst.Add(s);
                    i += 1;
                }
                foreach (KeyValuePair<int, List<string>> kvp in dic)
                {            }
      

  7.   


            static string[] Split(string str)
            {
                char[] crs = str.ToCharArray();
                List<string> ret = new List<string>();
                int i = 0;
                string tmp=string.Empty;
                foreach (char ch in crs)
                {
                    if (ch == ',')
                        continue;
                    tmp += ch;
                    if (i == 2)
                    {
                        ret.Add(tmp);
                        tmp = string.Empty;
                        i = 0;
                    }
                    else
                    {   
                        i++;
                    }
                }
                if (tmp.Length > 0)
                {
                    ret.Add(tmp);
                }
                return ret.ToArray();
            }我应该好好练习下算法了。哎
      

  8.   


            string newstr=null;
            string str = "df,122,12e,12g,12e,12d2,12d2,12d2,";
            str = str.Replace(",","");
            for (int i = 1; i < str.Length; i++)
            {
                newstr = newstr + str.Substring(i, 1);
                if (i % 3 == 0 && i !=0)
                {
                    newstr = newstr + ",";
                }
                
            }
            Response.Write(newstr);
      

  9.   


    非常感谢.viewstate.
    感觉还行
      

  10.   


    怎么引用不了呀,强类型是如何引用的.
    我的报错 CS0246: 找不到类型或命名空间名称“List”(是否缺少 using 指令或程序集引用?)
      

  11.   


                List<string> _L = new List<string>();
                while (str.IndexOf(",") > 0)
                {
                    int i = str.IndexOf(",", str.IndexOf(",", str.IndexOf(",") + 1) + 1);
                    string t = "";
                    if (i < 0)
                    {
                        t = str;
                        str = str.Remove(0, str.Length);
                    }
                    else
                    {
                        t = str.Substring(0, i);
                        str = str.Remove(0, i + 1);
                    }
                    _L.Add(t);
                }
                string[] sArry ={ _L[0], _L[1], _L[2] };            for (int i = 0; i < sArry.Length; i++)
                {
                    Console.WriteLine(sArry[i]);
                    Console.Write("");
                }
      

  12.   

    ”找不到类型或命名空间名称“List”(是否缺少 using 指令或程序集引用?)“用的楼上的list的DEMO吧      说的很明显了    缺少 using 指令或程序集引用
      

  13.   


    我加了这一句呀
    using System.Collections是不是还要加别的
      

  14.   

     /// <summary>
            /// 
            /// </summary>
            /// <param name="str">要分割的字符串</param>
            /// <param name="f">分割的组数</param>
            /// <returns></returns>
            static string[] _Result(string str, int f)
            {
                List<string> _L = new List<string>();
                string[] sArry = str.Split(',');            for (int i = 0; i < sArry.Length / f; i++)
                {
                    string _s = "";
                    for (int j = 0; j < sArry.Length; j++)
                    {
                        if (i == j / f)
                        {
                            _s += sArry[j].ToString() + ',';
                        }
                    }
                    _L.Add(_s);
                }
                return _L.ToArray();
            }
      

  15.   


     string str = "df,122,12e,12g,12e,12d2,12d2,12d2,";
                str=str.Replace(",","");
                 int count=0;
                 List<string> lists = new List<string>();
                 while (count <= str.Length-1)
                 {
                     if (count + 3 > str.Length)
                     {
                         lists.Add(str.Substring(count));
                     }
                     else
                     {
                         lists.Add(str.Substring(count, 3));
                     }
                     count += 3;
                 }
                 foreach (string list in lists)
                 {
                     Response.Write(list+"<br/>");
                 }
      

  16.   


    楼主用的.NET哪个版本?try...string str = "df,122,12e,12g,12e,12d2,12d2,12d2,";
    Regex reg = new Regex(@"([^,]*,){3}|([^,]*,)+$");
    MatchCollection mc = reg.Matches(str);
    foreach(Match m in mc)
    {
        richTextBox2.Text += m.Value + "\n";
    }