怎样对齐字符串啊??100分,马上结帐 "我爱csdn"="我爱csdn ";"我爱你" ="我爱你 ";用空格填充,两者位数要求一样。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 ][我爱你 ]*/ Console.WriteLine(String.Format("[{0,-12}人]","中国")); padright(' ',length);也可以实现吧。 不知道lz所说的对齐字符串,是不是简单在每个字符串后面加一个空格,如果这样的话,如下不是更简单:string strNew = strOld + " ";如果不是的话,请具体说说。 padright(' ',length);还是这个简单 对不齐,以上方法全部试过,如果中文字符和英文和数字混在一起肯定对不齐代码如下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);}}大家帮帮忙啊,分不够再加 问题:"我爱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 ][我爱你 ]*/ 怎么把ListBox1的每一行导出到字符串数组中? 请问: 异常详细信息: System.FormatException: 输入字符串的格式不正确。 c#窗体在调试运行时关闭窗体,但是显示依然在调试状态 C#中Debug.Write()输出到哪里了? 谁知道哪里有QQ里的那个分页控件下载,要C#的,找到给分!!!!!!! [ DataGrid绑定CheckBox以后的问题 三个小问题 能否在程序中创建存储过程? 二进制的问题 如果用windows.print()打印东西? 如何判断一个字符串中只有1个或2个","逗号
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 ]
[我爱你 ]
*/
也可以实现吧。
string strNew = strOld + " ";如果不是的话,请具体说说。
代码如下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);
}
}大家帮帮忙啊,分不够再加
"我爱你" ="我爱你 ";用空格填充,两者位数要求一样。--------------------------------------------答案: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 ]
[我爱你 ]
*/