在C#中使用String.Format格式化字符串的问题 [英] Problem with formatting a string with String.Format in C#

查看:283
本文介绍了在C#中使用String.Format格式化字符串的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以特定格式在消息框中打印一个字符串,为此,我正在使用类似于以下所示的代码:

I need to print a string in a message box in specific format for which i am using code similar to as shown below:

string text=""; 
for (int i=0; i<n; i++)
{
   a=..
   b=..
   c=..
   text += String.Format("{0, -8} {1,-4} {2,8}", a, b, c);
}
MessageBox.Show(text);

对于以下一组值:

XYZ,ABC,100

X,ABC,100

我得到以下输出:

XYZ     ABC     100

X     ABC     100

因此您可以看到第二行的格式不正确. 可能是因为我正在MessageBox中打印此内容而发生. 角色和空格"所占用的空间是不同的. 有什么解决办法吗?

So you can see the second line is not well formatted. Probably this is happening because i am printing this in MessageBox. The space a character and a 'space' takes is different. Any solution for this?

推荐答案

尝试使用\t在值之间插入制表符.

Try using a \t to insert tabs between values.

这篇关于在C#中使用String.Format格式化字符串的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆