我知道我可以填充空格:
String.format("%6s", "abc"); // ___abc ( three spaces before abc
Run Code Online (Sandbox Code Playgroud)
但我似乎无法找到如何生产:
000abc
Run Code Online (Sandbox Code Playgroud)
编辑:
我%06s先问过这个问题.只是在更多(未经验证的)答案出现之前让你知道.
目前我有:String.format("%6s", data ).replace(' ', '0' ) 但我认为必须存在更好的方法.
我正在尝试创建一个tab delimeted文本文件,以便输出显示为列,但由于某种原因,选项卡显示在不同的位置.这是由数据值不同的事实引起的.
这是我如何构建行和列
output.append("|\t" + column1 + "\t\t\t:\t" + column2 +" \t\t\n");
Run Code Online (Sandbox Code Playgroud)
输出结果如下
| activeSessions : 0
| duplicates : 0
| expiredSessions : 0
| rejectedSessions : 0
| sessionMaxAliveTime : 0
| sessionCounter : 0
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,第一列上包含较长文本条目的值会导致第二列稍微移动一点,即使两列都由两个选项卡分隔.如何确保第二列位置在同一行?
谢谢
朋友我必须在项目中捏造一些东西,我发现了一些困难,如下:
String name1 = "Bharath" // its length should be 12
String name2 = "Raju" // its length should be 8
String name3 = "Rohan" // its length should be 9
String name4 = "Sujeeth" // its length should be 12
String name5 = "Rahul" // its length should be 11 " Means all Strings with Variable length"
Run Code Online (Sandbox Code Playgroud)
我有字符串和它们的长度.我需要输出如下格式.通过使用字符串连接和填充.我需要在Groovy中回答,即使Java也很好..
"Bharath Raju Rohan Sujeeth Rahul "
Run Code Online (Sandbox Code Playgroud)
手段:
Bharath以5个黑色空格作为lenth是12(7 + 5 = 12),
Raju从4个黑色空间开始,因为lenth是8(4 + 4 = 8),
Rohan向前开了4个黑色空格,因为lenth是9(5 + 4),
Sujeeth向前5个黑色空间,因为lenth是12(7 …
我想使用 printf 在整齐的列中打印内容。
happening 0.083333 [4]
hi 0.083333 [0]
if 0.083333 [8]
important 0.083333 [7]
is 0.250000 [3, 5, 10]
it 0.166667 [6, 9]
tagged 0.083333 [11]
there 0.083333 [1]
what 0.083333 [2]
Run Code Online (Sandbox Code Playgroud)
我有这个代码System.out.printf("%s %.6f %s \n", word, web.getFrequency(word), loc);,但它打印出这个:
happening 0.083333 [ 4 ]
hi 0.083333 [ 0 ]
if 0.083333 [ 8 ]
important 0.083333 [ 7 ]
is 0.250000 [ 3 5 10 ]
it 0.166667 [ 6 9 ]
tagged 0.083333 [ 11 ] …Run Code Online (Sandbox Code Playgroud) 我有这样的速度模板:
+-----------------------------------------------------------+-------------+-------------+-------------+-------------+
| $totalPel Pelanggan | $totalBk | $totalAdm | $totalTag | $totalTotal |
+-----------------------------------------------------------+-------------+-------------+-------------+-------------+
Run Code Online (Sandbox Code Playgroud)
当我输入 $totalPel -> 100 时,结果是:
+-----------------------------------------------------------+-------------+-------------+-------------+-------------+
| 100 Pelanggan | $totalBk | $totalAdm | $totalTag | $totalTotal |
+-----------------------------------------------------------+-------------+-------------+-------------+-------------+
Run Code Online (Sandbox Code Playgroud)
其实我想得到这样的结果:
+-----------------------------------------------------------+-------------+-------------+-------------+-------------+
| 100 Pelanggan | $totalBk | $totalAdm | $totalTag | $totalTotal |
+-----------------------------------------------------------+-------------+-------------+-------------+-------------+
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我如何制作它?
我使用这个java代码:
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("totalPel", 100);
Template template = Velocity.getTemplate("LaporanTagihan.txt");
StringWriter writer = new StringWriter();
template.merge(context, writer);
System.out.println(writer.toString());
Run Code Online (Sandbox Code Playgroud) 根据我的要求,可能已经有某种apache或guava实用程序类了:
我想始终创建相同长度的字符串.丢失的字符应该用固定字符向左或向右填充.就像是:
Utils.filledString(teststring, " ", 5); //would ensure the teststring is always 5 chars long, and if not append whitespace to the right
Utils.filledString(teststring, "x", -5); //same as above, but fill the 5 chars left with an x
Run Code Online (Sandbox Code Playgroud)
你明白了,可能它已经存在,但我找不到正确的关键字来找到它.