假设我有一些变量,我想格式化它们以便它们全部对齐,但变量的长度不同。例如:
import matplotlib.pyplot as plt
x = [2,3,4,5,6,14,16,18,24,22,20]
y = [1, 4, 9, 6,5,6,7,8,9,5,7]
labels = ['12Am', '12Pm']
plt.plot(x, y, 'ro')
'12,24 are location on x-axis for labels'
plt.xticks((12,24),('12Am', '12Pm'))
plt.margins(0.2)
plt.show()
我也有代价。
String a = "abcdef";
String b = "abcdefhijk";
我怎么能够格式化它,所以不管字符串有多长,它们都以任何方式对齐?
double price = 4.56;
例如,上面的代码会输出如下内容:
System.out.format("%5s %10.2f", a, price);
System.out.format("%5s %10.2f", b, price);
但是我希望它输出这样的东西:
abcdef 4.56
abcdefhijk 4.56
我该怎么做?提前谢谢。
答案 0 :(得分:4)
使用固定大小格式:
使用固定大小的格式字符串允许在a中打印字符串 具有固定大小列的表格外观:
String rowsStrings[] = new String[] {"1", "1234", "1234567", "123456789"}; String column1Format = "%-3.3s"; // fixed size 3 characters, left aligned String column2Format = "%-8.8s"; // fixed size 8 characters, left aligned String column3Format = "%6.6s"; // fixed size 6 characters, right aligned String formatInfo = column1Format + " " + column2Format + " " + column3Format; for(int i = 0; i < rowsStrings.length; i++) { System.out.format(formatInfo, rowsStrings[i], rowsStrings[i], rowsStrings[i]); System.out.println(); }
输出:
1 1 1 123 1234 1234 123 1234567 123456 123 12345678 123456
在您的情况下,您可以找到要显示的字符串的最大长度,并使用它来创建适当的格式信息,例如:
// find the max length
int maxLength = Math.max(a.length(), b.length());
// add some space to separate the columns
int column1Length = maxLength + 2;
// compose the fixed size format for the first column
String column1Format = "%-" + column1Length + "." + column1Length + "s";
// second column format
String column2Format = "%10.2f";
// compose the complete format information
String formatInfo = column1Format + " " + column2Format;
System.out.format(formatInfo, a, price);
System.out.println();
System.out.format(formatInfo, b, price);
答案 1 :(得分:0)
您可以找到最长String
,然后使用Apache commons-lang StringUtils
到leftPad
两个String
。像,
int len = Math.max(a.length(), b.length()) + 2;
a = StringUtils.leftPad(a, len);
b = StringUtils.leftPad(b, len);
或者,如果您无法使用StringUtils
,则可以实施leftPad
。首先是生成空格String
的方法。像,
private static String genString(int len) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
sb.append(' ');
}
return sb.toString();
}
然后用它来实现leftPad
- 比如,
private static String leftPad(String in, int len) {
return new StringBuilder(in) //
.append(genString(len - in.length() - 1)).toString();
}
并且,我测试了它,
int len = Math.max(a.length(), b.length()) + 2;
System.out.format("%s %.2f%n", leftPad(a, len), price);
System.out.format("%s %.2f%n", leftPad(b, len), price);
哪些输出(我认为你想要的)
abcdef 4.56
abcdefhijk 4.56
答案 2 :(得分:0)
在您的格式说明符前放置负号,这样它就不会在浮点值的左侧打印5个空格,而是调整右边的空格,直到找到理想的位置。应该没问题
答案 3 :(得分:0)
private String addEnoughSpacesInBetween(String firstStr, String secondStr){
if (!firstStr.isEmpty() && !secondStr.isEmpty()){
String space = " ";
int totalAllowed = 55;
int multiplyFor = totalAllowed - (firstStr.length() + secondStr.length());
return StringUtils.repeat(space,multiplyFor);
}
return null;
}
答案 4 :(得分:0)
您可以通过以下方式实现它-
String a = "abcdef";
String b = "abcdefhijk";
double price = 4.56;
System.out.println(String.format("%-10s %-10.2f", a, price));
System.out.println(String.format("%-10s %-10.2f", b, price));
输出:
abcdef 4.56
abcdefhijk 4.56