使用%n作为换行符。

huangapple go评论77阅读模式
英文:

Using %n as new line

问题

如推荐的示例中所示:
https://stackoverflow.com/a/38077906/12340711
>最好使用%n作为与操作系统无关的换行符,而不是\n,而且比使用System.lineSeparator()更容易。

然而,该函数对我不起作用:

\n:

System.out.println("foo\nbar");
>>>foo
>>>bar

按预期运行。

%n:

System.out.println("foo%nbar");
>>>foo%nbar

未被解释。

有人能解释一下为什么 %n 对我不起作用吗?它可能不再受支持,还是我漏了什么?

英文:

as recommended as for example in this post:
https://stackoverflow.com/a/38077906/12340711
>It's better to use %n as an OS independent new-line character instead of \n and it's easier than using System.lineSeparator()

However, the function does not work for me:

\n:

System.out.println("foo\nbar");
>>>foo
>>>bar

works as it should.

%n:

System.out.println("foo%nbar");
>>>foo%nbar

is not interpreted.

Can somebody explain me why %n does not work for me? Is it perhaps not longer supported or am I missing something?

答案1

得分: 3

Java为您提供了System.lineSeparator()常量。如果您使用println,它将以与操作系统无关的方式换行。如果您只是在编写代码时尝试,您也可以在字符串中使用\n来换行。这很简单和快速。请注意,println只是将纯文本打印到控制台。

当您使用printf时,实际上将使用java.util.Formatter控制台输出之前对字符串进行格式化。关于java.util.Formatter的所有细节可以在(Java 8文档链接)找到:https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html

因此,如果您想要使用println格式化字符串,您需要首先使用java.util.Formatter对其进行格式化,例如:

String formattedStr = String.format("%s = %d %n hello", "joe", 35);
System.out.println(formattedStr);

其中%s表示字符串,%d表示整数,%n表示换行(在上面的文档链接中有描述)。

printf实际上是上述2行代码的快捷方式,其中String.format已经包含在printf的实现中。

System.out.printf("%s = %d %n hello", "joe", 35);

您还可以在printf字符串中使用\n

System.out.printf("%s = %d \n hello", "joe", 35);

最后,重要的是要理解,在实际项目中,如果您没有使用java.util.Formatter(在Formatter中使用%n),您应该始终使用System.lineSeparator()而不是\n。此外,您不应该在实际项目中使用System.out.*,而应改用日志记录框架。

英文:

Java provides you the System.lineSeparator() constant. That will break the line in a OS independent way if you are using println. You can also just use \n in a string to break the line if you are just playing around with code. It's easy and fast. Note that println just prints a pure text to the console.

When you use printf, you will actually be using a java.util.Formatter to format the string before the console output. All details of the java.util.Formatter can be found at (Java 8 docs link): https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html

So, if you want to format a string using println, you will need to format it first using java.util.Formatter with something like:

String formattedStr = String.format("%s = %d %n hello", "joe", 35);
System.out.println(formattedStr);

Where %s is a string, %d is a integer number and %n a line break (described in documentation link above).

printf is just a shortcut to the above 2 lines of code where the String.format is already inside printf implementation.

System.out.printf("%s = %d %n hello", "joe", 35);

And you can also use \n in a printf string:

System.out.printf("%s = %d \n hello", "joe", 35);

Finally, it is important for you to understand that in a real world project you should always use System.lineSeparator() instead \n if you are not using a java.util.Formatter (in a Formatter, use %n). Also, you should never use System.out.* for real projects. Use a logging framework instead.

答案2

得分: 2

%nSystem.out.println()解释为String

要使用%n,您必须使用System.out.printf(),就像您所发布的示例中那样。

注意\nSystem.out.printf()解释为换行

英文:

%n is interpreted as String by System.out.println().

To use %n, you must use System.out.printf(), like in the example you have posted.

Note: \n is interpreted as newline by System.out.printf().

huangapple
  • 本文由 发表于 2020年4月6日 05:51:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61050018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定