格式化包含许多字符串的长system.out.print&整数

时间:2016-01-22 19:15:18

标签: java

让我们说你有以下内容:

String cake = "whatever";
int cakeNumber = 509;
String whyNot = "another string";
double number = 1;

system.out.println("I like "+ cake +" and I eat " + cakeNumber + " blah blah     prolonging this string because " + whyNot + " and so on " + number + ".");

所以,这并没有多大意义,而且它并不是必须的,因为它只是一个例子。我的问题是这个,给出了多个字符串和变量以及' plus'他们之间,看起来像一团糟。是不是有办法格式化这个println或者通过减少加号来整理它?

6 个答案:

答案 0 :(得分:6)

java.lang.StringBuilder

System.out.println(new StringBuilder("I like")
                   .append(cake)
                   .append(" and I eat ")
                   .append(cakeNumber)
                   .append(" blah blah     prolonging this string because ")
                   .append(whyNot)
                   .append(" and so on ")
                   .append(number)
                   .append(".")
                   .toString());

java.text.MessageFormat

System.out.println(MessageFormat.format("I like {0} and I eat {1} blah blah    prolonging this string because {2} and so on {3}.",
                   cake, cakeNumber, whyNot, number));

我喜欢静态导入,如下所示:

import static java.text.MessageFormat.format;

System.out.println(format("I like {0} and I eat {1} blah blah    prolonging this string because {2} and so on {3}.",
                   cake, cakeNumber, whyNot, number));

java.util.Formatter(也称为String.format

System.out.printf("I like %s and I eat %d blah blah    prolonging this string because %s and so on %f.%n",
                  cake, cakeNumber, whyNot, number);

你可以在这里找到很多语法选择(只是列出一些,可能还有更多):

System.out.format(...)
System.out.printf(...)
System.out.print(String.format(...)) // you will need to include the line break in the format
System.out.println(String.format(...)) // line break will be caused by println()
import static java.lang.String.format;
System.out.print(format(...))
System.out.println(format(...))
System.out.println(new Formatter().format(...))

%n表示系统的特定换行符。这是必需的,因为printf不会在操作后自动插入换行符。

多行字符串连接

System.out.println("I like " + cake + " and I eat " +
                   cakeNumber + " blah blah    prolonging this string because" +
                   whyNot + " and so on " + number + ".");

答案 1 :(得分:2)

尝试"""<br> 2016-01-22|{INFO}: Starting $SOME_PROGRAM$ on $SOME_FILE$<br> program_output_line<br> program_output_line<br> program_output_line<br> program_output_line<br> program_output_line<br> 2016-01-22|{INFO}: Finished running $SOME_PROGRAM$<br> """

例如,你可以写

printf

另请注意,许多IDE(如Eclipse)将允许您轻松跨越多行的字符串。但System.out.printf("I like %s and I eat %d blah blah prolonging this string because %s and so on %0.f.\n", cake, cakenumber, whyNot, number); 的优点是您可以将字符串与变量分开,还可以更好地控制数字的显示。例如,请注意printf。通常,您可以指定值应占用多少空间,包括填充和要使用的小数位数。

答案 2 :(得分:0)

一个选项,如果你想减少线条和加号,你可以将其分解为System.out.print个:

System.out.print("I like "+ cake); 
System.out.print(" and I eat " + cakeNumber);
System.out.print(" blah blah     prolonging this string because ");
System.out.println(whyNot + " and so on " + number + ".");

答案 3 :(得分:0)

然后你可以尝试:

int age = 24;
double salary = 4500.25;
String name = "Marcelo";
String message = "Hi, my name is %s and I'm %d years old and I earn %f.";

System.out.println(String.format(message,name,age,salary));

请记住String.format使用默认语言环境:)

快乐的编码。

答案 4 :(得分:0)

使用String.formatMessageFormat

String cake = "whatever";
int cakeNumber = 509;
String whyNot = "another string";
double number = 1;

System.out.println(MessageFormat.format("{0}..{1}..{2}..", cake, cakeNumber, ..));

答案 5 :(得分:0)

您需要考虑的一些示例输出。

    Select TOP 1
     (Select count(*) as Count from table1) as count_1, 
     (select count(*) as Count from table2) as count_2
   From table1

当您开始想要对参数执行任何类型的非默认格式化(零填充,空间填充,仅在您的浮点值上显示2位数字等)时,printf变得非常有用。

Foo.java

$ javac Foo.java
$ java Foo

--- Original ---
I like whatever and I eat 509 blah blah     prolonging this string because another string and so on 1.0.

--- whitespace is your friend---
I like whatever and I eat 509 blah blah     prolonging this string because another string and so on 1.0.

--- Also, look into printf ---
I like "whatever" and I eat 00509 blah blah     prolonging this string because "another string" and so on  1.00