【发布时间】:2012-07-21 17:00:24
【问题描述】:
朋友们,我在项目中要暗示一些东西,我发现了一些困难,如下:
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"
我有字符串和它们的长度。我需要得到如下格式的输出。通过使用字符串连接和填充。我需要在 Groovy 中回答,即使 Java 也很好..
"Bharath Raju Rohan Sujeeth Rahul "
意思是:
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+5),
Rahul 向前 6 个黑色空格,长度为 11(5+6),
【问题讨论】:
-
空格是看不到有问题的。我在问题中已经解释过了。请任何人帮助我。
-
尝试使用
java.util.Formatter和String.format()来实现。