在 C# 中使用 String.Format 格式化字符串时出现问题

发布于 2024-09-07 15:33:17 字数 459 浏览 1 评论 0原文

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

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 中打印它。 字符和“空间”所占用的空间是不同的。 有什么解决办法吗?

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);

So for following set of values:

XYZ,ABC,100

X,ABC,100

I get following output:

XYZ     ABC     100

X     ABC     100

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十六岁半 2024-09-14 15:33:17

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

Try using a \t to insert tabs between values.

心头的小情儿 2024-09-14 15:33:17

这不起作用,因为 MessageBox 使用按比例间隔的字体,字母 M 比字母 l 宽得多。就像您现在正在阅读的这则消息中的内容一样。仅当使用固定间距字体显示时,您才能期望这样的对齐方式起作用。更改消息框字体是不合适的,它是系统设置。

您可以通过使用制表符来获得更好的结果:

text += String.Format("{0}\t{1}\t{2}", a, b, c);

但如果字段大小接近制表符大小,则它并不是万无一失的。请改用带有 View = Details 的 ListView。

This doesn't work because MessageBox uses a proportionally spaced font, the letter M is much wider than the letter l. Just like it is in this message your are reading now. You can only expect alignment like this to work if it is displayed with a fixed-pitch font. Changing the message box font is not appropriate, it is a system setting.

You can get it somewhat better by using tabs:

text += String.Format("{0}\t{1}\t{2}", a, b, c);

but it isn't fool-proof if the field size approaches the tab size. Use a ListView with View = Details instead.

旧竹 2024-09-14 15:33:17

不确定这是否确实是您的意思,但使用 等宽 字体,例如“快递新"。如果您已经这样做了,那么对这个明显的答案感到抱歉。

没关系:按照这个 线程 使用标准 MessageBox 是不可能的。也许一个选择是创建您自己的 MessageBox 类。

Not sure if it is actually what you mean, but use a monospaced font like "Courier New". If you already did, then sorry for this obvious answer.

Nevermind: it's not possible with the standard MessageBox accoding to this thread. Maybe then an option is to create your own MessageBox class.

驱逐舰岛风号 2024-09-14 15:33:17

使用以下代码在 Windows 应用程序中创建的测试:

    public void Test1()
    {
        List<List<String>> list = new List<List<string>>() { 
            new List<String>() { "XYZ", "ABC","100" },
            new List<String>() { "X", "ABC", "100"},
        };

        string text = "", a = "", b = "", c = "";
        for (int i = 0; i < list.Count; i++)
        {
            a = list[i][0];
            b = list[i][1];
            c = list[i][2];
            text += String.Format("{0, -8} {1,-4} {2,8}{3}", a, b, c, Environment.NewLine);
        }
        MessageBox.Show(text);
    }

执行您所说的操作,但在使用以下代码使用控制台应用程序检查后:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test1();
            Console.ReadKey();
        }

        public static void Test1()
        {
            List<List<String>> list = new List<List<string>>() { 
                new List<String>() { "XYZ", "ABC","100" },
                new List<String>() { "X", "ABC", "100"},
            };

            string text = "", a = "", b = "", c = "";
            for (int i = 0; i < list.Count; i++)
            {
                a = list[i][0];
                b = list[i][1];
                c = list[i][2];
                text += String.Format("{0, -8} {1,-4} {2,8}{3}", a, b, c, Environment.NewLine);
            }
            Console.WriteLine(text);
        }
    }
}

它执行您所期望的操作。

因此,测试表明,如果正在执行其应该执行的操作,但由于 MessageBox 中缺少相同宽度的字体,因此它无法正确对齐。但另一方面,当控制台应用程序使用相同宽度的字体时,它确实可以完全对齐。

A test created in Windows Application with the following code :

    public void Test1()
    {
        List<List<String>> list = new List<List<string>>() { 
            new List<String>() { "XYZ", "ABC","100" },
            new List<String>() { "X", "ABC", "100"},
        };

        string text = "", a = "", b = "", c = "";
        for (int i = 0; i < list.Count; i++)
        {
            a = list[i][0];
            b = list[i][1];
            c = list[i][2];
            text += String.Format("{0, -8} {1,-4} {2,8}{3}", a, b, c, Environment.NewLine);
        }
        MessageBox.Show(text);
    }

Does what you said, but after checking it with the console application with the following code:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test1();
            Console.ReadKey();
        }

        public static void Test1()
        {
            List<List<String>> list = new List<List<string>>() { 
                new List<String>() { "XYZ", "ABC","100" },
                new List<String>() { "X", "ABC", "100"},
            };

            string text = "", a = "", b = "", c = "";
            for (int i = 0; i < list.Count; i++)
            {
                a = list[i][0];
                b = list[i][1];
                c = list[i][2];
                text += String.Format("{0, -8} {1,-4} {2,8}{3}", a, b, c, Environment.NewLine);
            }
            Console.WriteLine(text);
        }
    }
}

It does what you expect.

So, what the tests suggest is if is doing what it is supposed to do, but with the lack of same width font in the MessageBox, it does not line up properly. But on the other hand, with the console application using the same width fonts, it does line up exactly.

离旧人 2024-09-14 15:33:17

MessageBox 类,无论是来自 Forms 还是 WPF,都只是 win32 消息框的包装器,因此程序员无法(轻松)执行诸如将字体更改为固定间距字体以使所有字符与字符串格式化。

但是,您可以使用表单和标签(以及您需要的任何按钮)创建自己的 MessageBox 克隆,然后使用 ShowDialog() 方法显示它。

The MessageBox class, whether from Forms or WPF, is just a wrapper around the win32 message box, so a programmer isn't able to (easily) do things like change the font to a fixed-pitch font so all characters line up nicely with string formatting.

You could, however, make your own clone of MessageBox using a Form and a Label (and whatever buttons you need), then show it using the ShowDialog() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文