带空格的 Java 字符串填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11612866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java String Padding with spaces
提问by Bharath A N
Friends I have to impliment something in project, I found some difficulties, and is as follows:
朋友我在项目中要实现一些东西,我发现了一些困难,如下:
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"
I have the Strings and their Lengths.I need to get output as in the below format.By using string concatination and padding.I need answer in Groovy, Even if Java also it is fine..
我有字符串和它们的长度。我需要得到如下格式的输出。通过使用字符串连接和填充。我需要在 Groovy 中得到答案,即使 Java 也很好..
"Bharath Raju Rohan Sujeeth Rahul "
Means:
方法:
Bharath onwards 5 black spaces as lenth is 12 (7+5 = 12),
Bharath 向前 5 个黑色空间,因为长度为 12 (7+5 = 12),
Raju onwards 4 black spaces as lenth is 8 (4+4 = 8),
Raju 向前 4 个黑色空间,因为长度为 8 (4+4 = 8),
Rohan onwards 4 black spaces as lenth is 9(5+4),
Rohan 起 4 个黑色空间,长度为 9(5+4),
Sujeeth onwards 5 black spaces as lenth is 12 (7+5),
Sujeeth 向前 5 个黑色空间,因为长度为 12 (7+5),
Rahul onwards 6 black spaces as lenth is 11(5+6),
Rahul 起 6 个黑色空间,长度为 11(5+6),
回答by tim_yates
You could do this:
你可以这样做:
// A list of names
def names = [ "Bharath", "Raju", "Rohan", "Sujeeth", "Rahul" ]
// A list of column widths:
def widths = [ 12, 8, 9, 12, 11 ]
String output = [names,widths].transpose().collect { name, width ->
name.padRight( width )
}.join()
Which makes output
equal to:
这使得output
等于:
'Bharath Raju Rohan Sujeeth Rahul '
Assuming I understand the question... It's quite hard to be sure...
假设我理解这个问题......很难确定......
回答by npinti
Take a look at Apache's StringUtils. It has methods to pad with spaces (either left or right).
看看 Apache 的StringUtils。它具有填充空格(左或右)的方法。
回答by astimony
You can use sprintf
, which is added to the Object class so it is always available:
您可以使用sprintf
,它已添加到 Object 类中,因此它始终可用:
def s = sprintf("%-12s %-8s %-9s %-12s %-11s", name1, name2, name3, name4, name5)
assert s == "Bharath Raju Rohan Sujeeth Rahul "
The formatting string used with sprintf
is the same as the format string used for the Formatter
class. See the JDK documentation for the format stringfor more information.
使用的格式字符串与sprintf
用于Formatter
类的格式字符串相同。有关格式字符串的更多信息,请参阅JDK 文档。
回答by Xvolks
As already said, you can use String.format() method to achieve your goal.
如前所述,您可以使用 String.format() 方法来实现您的目标。
For example :
例如 :
String[] strings = {
"toto1",
"toto22",
"toto333",
"toto",
"totoaa",
"totobbb",
"totocccc",
};
String marker = "01234567890|";
String output = "";
for(String s : strings) {
output += marker;
}
System.out.println(output);
output = "";
for(String s : strings) {
output += String.format("%-12s", s);
}
System.out.println(output);
This will output a first line with markers for 12 chars then 2nd line with expected string :
这将输出带有 12 个字符标记的第一行,然后是带有预期字符串的第二行:
01234567890|01234567890|01234567890|01234567890|01234567890|01234567890|01234567890|
toto1 toto22 toto333 toto totoaa totobbb totocccc