表格列转行

需求

将"11,21,31$12,22,32$13,23,33$14,24,34";

转换为:"11,12,13,14$21,22,23,24$31,32,33,34";

分析:

其实就是如下的表格的列转行:

11,21,31
12,22,32
13,23,33
14,24.34
... ...

 

11,12,13,14,1...
21,22,23,24,2...
31,32,33,34,3...
... ...

思路:将所有元素列出来,取定长间隔的元素即可,实现代码:

using System; using System.Collections.Generic; namespace adas { class Program { public static void Main(string[] args) { string orgString="11,21,31$12,22,32$13,23,33$14,24,34"; // 0,3,6,9 // 1,4,7,10 // 2,5,8,11 string[] orgStrArr=orgString.Split(new char[]{'$'},StringSplitOptions.RemoveEmptyEntries); List<string >strList=new List<string> (); int rowNum=orgStrArr.Length; int colNum=0; foreach(string str in orgStrArr) { string[] itemArr=str.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries); colNum=itemArr.Length; foreach(string item in itemArr) { strList.Add(item); } } for(int i=0;i<rowNum-1;i++) { for(int j=i;j<strList.Count;j=j+colNum) { Console.WriteLine(strList[j]); } } Console.Read(); } } }

   for(int i=0;i<rowNum-1;i++)
   {
    for(int j=i;j<strList.Count;j=j+colNum)
    {
     Console.WriteLine(strList[j]);     
    }
   }

你可能感兴趣的:(String,Class)