Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何调整尾随空格?_Java - Fatal编程技术网

Java 如何调整尾随空格?

Java 如何调整尾随空格?,java,Java,我正在用java编写一个文件,但是输入到文件中的字符串是不同的,如何根据字符串的长度调整尾随空格 比如说 First Name Last Name Address ---------- --------- ------- Michael Jordan 23 E. Jump Street Larry Bird 33 North Celtics Run 您可以使用: 将为您提供: [foo

我正在用java编写一个文件,但是输入到文件中的字符串是不同的,如何根据字符串的长度调整尾随空格

比如说

First Name     Last Name     Address
----------     ---------     -------
Michael        Jordan        23 E. Jump Street
Larry          Bird          33 North Celtics Run
您可以使用:

将为您提供:

[foo                 ]

如果您试图用填充文件写入文件,那么考虑使用PrtuWrrter编写文件,并使用它的PrtTf(…)方法。API将告诉您如何使用它

e、 g

公共类PrintfExample{
公共静态void main(字符串[]args){
字符串格式字符串=“%-14s%-14s%-14s%n”;
字符串[][]数据={{{“名字”、“姓氏”、“地址”},
{"---------", "---------", "---------"},
{“史密斯”、“约翰”、“缅因街100号”},
{“迈克尔”、“乔丹”、“东跳街23号”},
{“拉里”,“伯德”,“33北凯尔特人跑”};
对于(int i=0;i
或者像这样,只需使用substring():


不知道你在问什么。你的意思是“如何将所有字符串填充到相同的长度”?这两个
PrintStream.printf()
String.format()
都使用相同的
java.util.Formatter()
类。@MichałŠrajer:是的,我同意。顺便说一句,为你的优秀答案投1+票。在Java5中,引入了
java.util.Formatter
来阻止人们这样做。无意冒犯,但不要实现标准库中已有的功能,除非您确实需要支持java5之前的代码;2) 它可以使用可变数量的值1.1)[java]是这个问题中唯一的标记。1.2)许多语言都有类似printf的格式化。2) 您也可以将格式化程序与可变数量的值一起使用。
[foo                 ]
public class PrintfExample {
   public static void main(String[] args) {
      String formatString = "%-14s %-14s %-14s%n";
      String[][] data = {{"First Name", "Last Name", "Address"},
            {"---------", "---------", "---------"},
            {"Smith", "John", "100 Main St"}, 
            {"Michael", "Jordan", "23 E. Jump Street"},
            {"Larry", "Bird", "33 North Celtics Run"}};

      for (int i = 0; i < data.length; i++) {
         // you would be writing to a File wrapped in a PrintWriter using printf
         // instead of to System.out (which is in fact a PrintWriter object),
         // but the technique is the same.
         System.out.printf(formatString, data[i][0], data[i][1], data[i][2]);
      }
   }
}
public class FormatFields {

private static final String [] [] data = {
        {"First Name", "Last Name", "Address"},
        {"----------", "---------", "-------"},
        {"Michael", "Jordan", "23 E. Jump Street"},
        {"Larry", "Bird", "33 North Celtics Run"}
};


private static final int FIELD_LENGTH = 20;
private static final String PADDING = "                    "; // 20 spaces

/**
 * @param args
 */
public static void main(String[] args) {

    for (int i = 0; i < data.length; i++) {
        for (int j = 0; j < data[i].length; j++) {

            System.out.print((data[i][j] + PADDING).substring(0, FIELD_LENGTH));

        }
        System.out.println();
    }
}

}
First Name          Last Name           Address             
----------          ---------           -------             
Michael             Jordan              23 E. Jump Street   
Larry               Bird                33 North Celtics Run