"我爱csdn"="我爱csdn    ";
"我爱你"  ="我爱你      ";用空格填充,两者位数要求一样。

解决方案 »

  1.   

    using System;
    using System.Text;class StrUtl
    {
      // 把一个“unicode字符串”格式化为全角字符占2个位置的“普通字符串”
      // width<0 表示左对齐,右边填空格
      // width>0 表示右对齐,左边填空格
      public static string StrFormat(string s, int width)
      {
        Encoding code = Encoding.GetEncoding("GB18030");
        byte [] bs = code.GetBytes(s);
        bool leftAlign = (width < 0 ? true : false);
        if (width < 0) width = - width;
        if (bs.Length >= width) return s;
        if (leftAlign) return s.PadRight(width-bs.Length+s.Length);
        return s.PadLeft(width-bs.Length+s.Length);
      } static void Main()
    {
        string s1 = "我爱csdn";
        string s2 = "我爱你";
        Console.WriteLine("[" + StrFormat(s1, -12) + "]");
        Console.WriteLine("[" + StrFormat(s2, -12) + "]");
    }
    }
    /* 程序输出:
    [我爱csdn    ]
    [我爱你      ]
    */
      

  2.   

    Console.WriteLine(String.Format("[{0,-12}人]","中国"));
      

  3.   

    padright(' ',length);
    也可以实现吧。
      

  4.   

    不知道lz所说的对齐字符串,是不是简单在每个字符串后面加一个空格,如果这样的话,如下不是更简单:
    string strNew = strOld + " ";如果不是的话,请具体说说。
      

  5.   

    padright(' ',length);还是这个简单
      

  6.   

    对不齐,以上方法全部试过,如果中文字符和英文和数字混在一起肯定对不齐
    代码如下private void printer()
    {string ps="&&" +"*********消费计划凭证**********";
    ps = ps + "@@" + "-----------------------------------";
    ps = ps + "@@" + "单据编号  :" + DJNO;
    ps = ps + "@@" + "公司名称  :" + userdata._corpname;            
    ps = ps + "@@" + "部门名称  :" + userdata._deptname;
    ps = ps + "@@" + "持卡人    :" + userdata._name ;
    ps = ps + "@@" + "当年限额  :" + lbl_FirstMoney.Text;
    ps = ps + "@@" + "当年消费  :" + lbl_LastM.Text;
    ps = ps + "@@" + "序号 |项目名称      |数量   |单价  |总价";
    foreach(DataRow dr in dg_source.Tables[0].Rows)
    {
    string xmname=dr["XMNAME"].ToString();
    int length=xmname.Length;
    string xmnum=dr["XMNUM"].ToString();
    string danjia=dr["XMDANJIA"].ToString();
    string totalprice=dr["XMTOTALPRICE"].ToString();
    ps=ps + "@@" + i.ToString("00")+"  "+string.Format("|{0,-10}|{1,-6}|{2,-6}|{3}",xmname,xmnum,danjia,totalprice);
    }
    }大家帮帮忙啊,分不够再加
      

  7.   

    问题:"我爱csdn"="我爱csdn    ";
    "我爱你"  ="我爱你      ";用空格填充,两者位数要求一样。--------------------------------------------答案:using System;
    using System.Text;class StrUtl
    {
      // 把一个“unicode字符串”格式化为全角字符占2个位置的“普通字符串”
      // width<0 表示左对齐,右边填空格
      // width>0 表示右对齐,左边填空格
      public static string StrFormat(string s, int width)
      {
        Encoding code = Encoding.GetEncoding("GB18030");
        byte [] bs = code.GetBytes(s);
        bool leftAlign = (width < 0 ? true : false);
        if (width < 0) width = - width;
        if (bs.Length >= width) return s;
        if (leftAlign) return s.PadRight(width-bs.Length+s.Length);
        return s.PadLeft(width-bs.Length+s.Length);
      }static void Main()
    {
        string s1 = "我爱csdn";
        string s2 = "我爱你";
        Console.WriteLine("[" + StrFormat(s1, -12) + "]");
        Console.WriteLine("[" + StrFormat(s2, -12) + "]");
    }
    }
    /* 程序输出:
    [我爱csdn    ]
    [我爱你      ]
    */