如何在 Java 中打印添加换行符的字符串?
我有一个字符串,
"I am a boy".
我想以这种方式打印它,
"I
am
a
boy".
有人可以帮助我吗?
I have a string like
"I am a boy".
I would like to print it this way
"I
am
a
boy".
Can anybody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
您还可以使用
System. lineSeparator()
:You can also use
System.lineSeparator()
:示例
输出
说明
最好使用
%n
作为操作系统独立的换行符,而不是\n
,并且比使用System.lineSeparator()
为什么要使用
%n
,因为在每个操作系统上,新行引用不同的字符集;LF是换行的缩写,CR是回车的缩写。转义字符写在括号内。因此,在每个操作系统上,新行代表系统特定的内容。
%n
与操作系统无关,它是可移植的。它代表 Unix 系统上的\n
或 Windows 系统上的\r\n
等等。因此,不要使用\n
,而是使用%n
。Example
Output
Explanation
It's better to use
%n
as an OS independent new-line character instead of\n
and it's easier than usingSystem.lineSeparator()
Why to use
%n
, because on each OS, new line refers to a different set of character(s);LF is the acronym of Line Feed and CR is the acronym of Carriage Return. The escape characters are written inside the parenthesis. So on each OS, new line stands for something specific to the system.
%n
is OS agnostic, it is portable. It stands for\n
on Unix systems or\r\n
on Windows systems and so on. Thus, Do not use\n
, instead use%n
.可以通过多种方式完成。我提到了两种简单的方法。
非常简单的方法如下:
也可以通过连接来完成,如下所示:
It can be done several ways. I am mentioning 2 simple ways.
Very simple way as below:
It can also be done with concatenation as below:
尝试:
Try:
为了使代码可移植到任何系统,我将使用:
这很重要,因为不同的操作系统使用不同的换行符号:Windows 使用“\r\n”,经典 Mac 使用“\r”,Mac 和 Linux 都使用“\ n”。
评论者 - 如果我的观点有误,请纠正我......
To make the code portable to any system, I would use:
This is important because different OSs use different notations for newline: Windows uses "\r\n", Classic Mac uses "\r", and Mac and Linux both use "\n".
Commentors - please correct me if I'm wrong on this...
\n
用于分隔行;示例:
结果:
\n
is used for making separate line;Example:
Result:
与平台无关的换行符:
输出:
注释:
System.getProperty("line.separator")
System.getProperty("line.separator")
上面:System.lineSeparator()
Platform-Independent Line Breaks:
Output:
Notes:
System.getProperty("line.separator")
System.lineSeparator()
如果您只想在控制台中打印换行符,可以使用
\n
来换行符。如果您想在 Swing 组件中中断文本,可以使用 HTML 及其
:If you simply want to print a newline in the console you can use
\n
for newlines.If you want to break text in Swing components you can use HTML and its
<br>
:如果你想让你的代码与操作系统无关,你应该为每个单词使用 println
,因为 Windows 使用“\r\n”作为换行符,而 unixoid 系统只使用“\n”
println 总是使用正确的换行符
If you want to have your code os-unspecific you should use println for each word
because Windows uses "\r\n" as newline and unixoid systems use just "\n"
println always uses the correct one
%n
使用像String.format()
这样的格式化程序怎么样?:如 这个答案说,它从 java 1.5 开始提供,是
System.getProperty("line.separator")
或的另一种方式System.lineSeparator()
并且,像这两个一样,它是操作系统独立的。What about
%n
using a formatter likeString.format()
?:As this answer says, its available from java 1.5 and is another way to
System.getProperty("line.separator")
orSystem.lineSeparator()
and, like this two, is OS independent.完整的程序示例,有一个有趣的变化:
打开一个新的空白文档并将其另存为
%yourJavaDirectory%/iAmABoy/iAmABoy.java
。 “iAmABoy”是班级名称。将以下代码粘贴并阅读。请记住,我是初学者,所以我感谢所有反馈!
现在,在 Terminal/cmd 中,浏览到
%yourJavaDirectory%/iAmABoy
并输入:您可以将参数
I am a boy
替换为任何内容!Full program example, with a fun twist:
Open a new blank document and save it as
%yourJavaDirectory%/iAmABoy/iAmABoy.java
. "iAmABoy" is the class name.Paste the following code in and read through it. Remember, I'm a beginner, so I appreciate all feedback!
Now, in terminal/cmd, browse to
%yourJavaDirectory%/iAmABoy
and type:You can replace the args
I am a boy
with anything!分手。
Go for a split.
我使用此代码
String result = args[0].replace("\\n", "\n");
与终端我可以使用 arg
I\\nam\\na\ \boy
使System.out.println
打印出I use this code
String result = args[0].replace("\\n", "\n");
with terminal I can use arg
I\\nam\\na\\boy
to makeSystem.out.println
print out这里是!!
NewLine 称为CRLF(回车换行)。
示例:
结果:
它对我有用。
Here it is!!
NewLine is known as CRLF(Carriage Return and Line Feed).
Sample:
Result:
It worked for me.
您可以在字符串中使用
标记在 html 页面中显示you can use
<br>
tag in your string for show in html pages这里我使用的是 split 函数。我从空格中制动了字符串。然后我使用 println 函数并打印该值。
Here I am using the split function. I braked String from spaces. then I used println function and printed the value.