notepad++ 的 nppexec 中的 \t 间距

\t spacing in notepad++'s nppexec

我在 java "CircleDemo" 中写了一个程序,不可避免地会这样做:

System.out.printf("The circle's area \t   is \t %.2f \n", circle.getArea());
System.out.printf("The circle's diameter \t   is \t %.2f \n", circle.getDiameter());
System.out.printf("The circle's circumference is \t %.2f \n", circle.getCircumference());

在 cmd.exe 中显示如下:

The circle's area          is    3.14 
The circle's diameter      is    2.00
The circle's circumference is    6.28

漂亮、干净的格式。

但是,Notepad++ 的控制台 (nppexec) 会打印出相同的程序,如下所示:

The circle's area     is    3.14 
The circle's diameter      is    2.00
The circle's circumference is    6.28

您可以看到格式有何不同。现在,我已经玩了足够长的时间来找到导致这种情况的原因是“\t”在 cmd.exe 和 Notepad++ 的 nppexec.

中打印标签的方式不同。

我如何编辑 nppexec 的“\t”格式来打印“\t”,就像 cmd.exe 一样?

您可以试试下面的例子。请在宽度前使用“-”以确保左缩进。默认情况下,它们是右缩进的;这可能不适合您的目的。

    System.out.printf("%-30s %-5s $%.2f\n","The circle's area ", "is" ,circle.getArea());
    System.out.printf("%-30s %-5s $%.2f\n","The circle's diameter ", "is" ,circle.getDiameter());
    System.out.printf("%-30s %-5s $%.2f\n","The circle's circumference ", "is" ,circle.getCircumference());

来源:http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

避免使用\t获取数组格式。我可能会简单地使用

System.out.printf("The circle's area          is %.2f %n", circle.getArea() );
System.out.printf("The circle's diameter      is %.2f %n", circle.getDiameter());
System.out.printf("The circle's circumference is %.2f %n", circle.getCircumference());

如果需要更动态的解决方案,您可以使用

System.out.printf("The circle's %-13s is %.2f%n", "area", circle.getArea());

或者如果响应不需要总是以 The circle's

开头
System.out.printf("%-26s is %.2f%n", "The circle's area",  circle.getArea());

DEMO