一个字段为代码(如:01,001,0001,M01,M00001,...),另一个字段为名称(张三,王八蛋,张真人,山本一郎,大山,...)。
要把字段代码和字段名称拼成一行,并把每列左对齐,请问用c#如何实现?! 谢谢!
效果如下:
01        张三
001       王八蛋
00001     张真人
M01       山本一郎
M00001    大山

解决方案 »

  1.   

    显示到文本?控制台?还是form?
      

  2.   

                String str1 = "01,001,0001,M01,M00001";
                String str2 = "张三,王八蛋,张真人,山本一郎,大山";            String[] arr1 = str1.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                String[] arr2 = str2.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);            for (Int32 i = 0; i < arr1.Length; i++)
                {
                    Console.WriteLine(arr1[i].PadRight(15) + arr2[i].PadRight(15));
      

  3.   

    我写的时候认为你的两个里面的项数相同了
    ps:如果不相同的话,i < arr1.Length 就要改成 < 两个数组中较小的Length
      

  4.   

    string s="";
    Dictionary<string, string> dic= new Dictionary<string, string>(); 
    string[] arr=s.Split(new string[] { ","}, StringSplitOptions.RemoveEmptyEntries);
    string[] arr1=s.Split(new string[] { ","}, StringSplitOptions.RemoveEmptyEntries);
    for(int i=0;i<arr.length;i++)
    {
    dic.Add(arr[i], arr1[i]);
    }
      

  5.   

    如果要保存的话,可以存在一个List<String>中
                String str1 = "01,001,0001,M01,M00001";
                String str2 = "张三,王八蛋,张真人,山本一郎,大山";            String[] arr1 = str1.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                String[] arr2 = str2.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);            List<String> list = new List<String>();            for (Int32 i = 0; i < arr1.Length; i++)
                {
                    String temp = arr1[i].PadRight(15) + arr2[i].PadRight(15);
                    list.Add(temp);
                }            foreach (String s in list)
                    Console.WriteLine(s);
      

  6.   

    sorry...
    这样可以了吧。            String str1 = "01,001,0001,M01,M00001";
                String str2 = "张三,王八蛋,张真人,山本一郎,大山";            String[] arr1 = str1.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                String[] arr2 = str2.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);            Int32 maxL = 0;
                for (Int32 i = 0; i < arr1.Length; i++)
                {
                    if (arr1[i].Length > maxL)
                        maxL = arr1[i].Length;
                }            List<String> list = new List<String>();            for (Int32 i = 0; i < arr1.Length; i++)
                {
                    String temp = arr1[i].PadRight(maxL + 5) + arr2[i];
                    list.Add(temp);
                }            foreach (String s in list)
                    Console.WriteLine(s);
      

  7.   


    String str1 = "01,001,0001,M01,M00001";
      String str2 = "张三,王八蛋,张真人,山本一郎,大山";  String[] arr1 = str1.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
      String[] arr2 = str2.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    int maxlength =0 ;
    for(Int32 i = 0;i<arr1.Length; i++)
    {
    if(arr1[i].Length>maxlength)
    {
    maxlength = arr1[i].Length;
    }
    }
    int length = arr1.Length < arr2.Length ? arr1.Length : arr2.Length;
      for (Int32 i = 0; i < length; i++)
      {
      Console.WriteLine(arr1[i].PadRight(maxlength+2) + arr2[i]);
      }
            
      

  8.   

                String str1 = "01,001,0001,M01,M00001";
                String str2 = "张三,王八蛋,张真人,山本一郎,大山";            String[] arr1 = str1.Split(',' );
                String[] arr2 = str2.Split(',' );            for (Int32 i = 0; i < arr1.Length; i++)
                {                Console.WriteLine(arr1[i].PadRight(10) + arr2[i].PadRight(10));            }这样也可以啊! 不知道你们要加那么多没用的东西干嘛!
      

  9.   

    static void lll1()
    {
        String str1 = "01,001,0001,M01,M00001";
        String str2 = "张三1,王八蛋12,张真人,山本一太郎acd,大山";    String[] arr1 = str1.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        String[] arr2 = str2.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);    for (int i = 0; i < arr1.Length; i++)
        {
            int w = 20 - (Encoding.Default.GetByteCount(arr2[i]) - arr2[i].Length);
            Console.WriteLine(arr2[i].PadRight(w) + arr1[i].PadRight(15));
        }
    }运行结果:
    张三1               01
    王八蛋12            001
    张真人a             0001
    山本一太郎acd       M01
    大山                M00001
    程序执行结束,按任意键关闭窗口!
      

  10.   

    String str1 = "01,001,0001,M01,M00001";
      String str2 = "张三,王八蛋,张真人,山本一郎,大山";  String[] arr1 = str1.Split(',' );
      String[] arr2 = str2.Split(',' );  for (Int32 i = 0; i < arr1.Length; i++)
      {  Console.WriteLine(arr1[i].PadRight(10) + arr2[i].PadRight(10));  }
      

  11.   

    解析后直接输到table里面~嘻嘻
      

  12.   

    PadLeft  
    PadRight或者{0:10} {0:-10}