假如  str = "abcdefghijklmn"如何 将他 按 每N个切割成数组如 N=4      切成
abcd
efgh
ijkl
mn-------------------------------
请注意 高效率,可以试试 当str 有1w、10w、100w的长度时,效率如何c#/vb/dephi/js/....  都可以。

解决方案 »

  1.   

        class Program
        {
            static void Main(string[] args)
            {
                string str = "abcdefghijklmn";
                List<string> childs = new List<string>();
               
                foreach (string r in Cut(str, 4))
                {
                    Console.WriteLine(r);
                }            Console.ReadLine();
            }        static string[] Cut(string str, int len)
            {
                int rcount = Math.Max(0, (str.Length - 1) / len + 1);
                string[] results = new string[rcount];            for (int i = 0; i < results.Length-1; i++)
                {
                    results[i] = str.Substring(i * len, len);
                }
                results[results.Length - 1] = str.Substring((results.Length - 1) * len);            return results;
            }
        }怎么CSDN的插入代码的功能时有时无啊?
      

  2.   

    再试试:    class Program
        {
            static void Main(string[] args)
            {
                string str = "abcdefghijklmn";
               
                foreach (string r in Cut(str, 4))
                {
                    Console.WriteLine(r);
                }            Console.ReadLine();
            }        static string[] Cut(string str, int len)
            {
                int rcount = Math.Max(0, (str.Length - 1) / len + 1);
                string[] results = new string[rcount];            for (int i = 0; i < results.Length-1; i++)
                {
                    results[i] = str.Substring(i * len, len);
                }
                results[results.Length - 1] = str.Substring((results.Length - 1) * len);            return results;
            }
        }
      

  3.   

    最简单的
    for循环 效率不详int count=1;
    int N;
    if(str.Length%N==0)
    count=str.Length/N;
    else
    count=str.Length/N+1;
    string[]s=new string[count];
    for(int i=0;i<count-1;i++)
    {
       s[i]=str.Substring(N*i,N);
    }
    s[count-1]=str.Substring(N*(count-1));
      

  4.   

    至于性能,测试了一下一个1000多万的字符串,耗时1.04秒左右。当然我的机器以现在的标准属于垃圾的那种。        static void Main(string[] args)
            {
                string str = String.Join("", Enumerable.Range(1, 1000000).Select(i => "abcdefghijklmn").ToArray());
                Console.WriteLine("LEN=" + str.Length);            Stopwatch sw = new Stopwatch();
                sw.Start();            Console.WriteLine(Cut(str, 4).Length);            sw.Stop();            Console.WriteLine(sw.Elapsed.TotalSeconds);            //foreach (string r in Cut(str, 4))
                //{
                //    Console.WriteLine(r);
                //}            Console.ReadLine();
            }
      

  5.   

    那就不要用string改用stringbuild
      

  6.   


    相当于多了一个SPLIT,估计还要慢。。
      

  7.   

    char[] arr = str.ToCharArrray();然后用寻址方式. 没测试过.不知道性能杂样
      

  8.   

    string s = Console.ReadLine();
                int i = 0;
                string s1 = "";
                for (int j = 0; j < s.Length; j++)
                {
                    
                    if (i == 4)
                    {
                        i = 0;
                    }
                    s1 = s.Substring(i, 4);
                    Console.WriteLine(s1);
                    i++;
                }
                Console.WriteLine(s.Substring(s.Length-(s.Length%4),(s.Length%4)));
      

  9.   

    刚才发的那个错了   这个可以 
    string s = Console.ReadLine();
                int i = 0;
                string s1 = "";
                for (int j = 0; j < (s.Length/4); j++)
                {
                    i = j*4;
                    
                    s1 = s.Substring(i, 4);
                    
                    Console.WriteLine(s1);
                }            Console.WriteLine(s.Substring((s.Length / 4) * 4, (s.Length % 4)));