我现在要往一个txt里面写内容比如有数据a=["上海","北京","广州",呼和浩特"]
b=["1","2","3",4"]写到文本中就是WriteLine(a[i]+"\t"+b[i]);例如这样:上海 1
北京 2
广州 3
呼和浩特 4
但是TAB却对不齐

解决方案 »

  1.   

    \t制表符可以有不同的解释,而空格则比较通用,更容易控制对齐。
    WriteLine(string.Format("{0,-8}{1}", a[i], b[i]));{0,-8}中的8指展位的长度,负号指左对齐。
      

  2.   

    用tab不好,确实有时候对不齐
    直接用空格
      

  3.   

    "\t\t\t"
    制表符会根据前面字的多少而决定在哪一列,所以需要多个制表符连起来大于前面字符的长度才能对齐。
      

  4.   

    页面显示换行
    <%
    response.write "<BR>"
    response.write "换行1"
    response.write "<BR>"
    response.write "换行2"
    response.write "<BR>"
    %>
    源代码换行
    <%
    response.write "<BR>"
    response.write "换行3"
    response.write Chr(13)
    response.write "换行4"
    %>
      

  5.   

    我的意思就是和 table 一样
    上海           1
    北京           2
    呼和浩特       3
    广州           4
    乌鲁木齐       5
    张家界         6这样的方式对齐不管前面的文字有多长 后面的数字始终是对齐的
      

  6.   

    WriteLine(a[i].PadRight(20)+b[i]); 
      

  7.   


                string[] a = { "上海", "北京", "广州", "呼和浩特" };
                string[] b = { "1", "2", "3", "4" };
                int max = 0;
                foreach (string s in a)
                {
                    int length = System.Text.Encoding.Unicode.GetByteCount(s);
                    if (length > max)
                        max = length;
                }
                using (StreamWriter sw = new StreamWriter("F:\\BBB.txt"))
                {
                    for (int i = 0; i < a.Length; i++)
                    {
                        List<byte> list = new List<byte>(System.Text.Encoding.Unicode.GetBytes(a[i]));
                        for (int j = list.Count; j < max + 4; j++)
                        {
                            list.Add(32);
                            list.Add(0);
                        }
                        sw.WriteLine(System.Text.Encoding.Unicode.GetString(list.ToArray()) + b[i]);
                    }
                    sw.Close();
                }