Java 字符串新行

我有一根像这样的线

"I am a boy".

我想用这种方式印刷

"I
am
a
boy".

有人能帮我吗?

1077252 次浏览

试试:

System.out.println("I\nam\na\nboy");
System.out.println("I\nam\na\nboy");


System.out.println("I am a boy".replaceAll("\\s+","\n"));


System.out.println("I am a boy".replaceAll("\\s+",System.getProperty("line.separator"))); // portable way

如果您只是想在控制台中打印换行符,那么可以使用 \n来打印换行符。

如果你想在 Swing 组件中分解文本,你可以使用 HTML 及其 <br>:

String str = "<html>first line<br>second line</html>";

如果您想让您的代码具有非特定性,那么应该对每个单词使用 println

System.out.println("I");
System.out.println("am");
System.out.println("a");
System.out.println("boy");

因为 Windows 使用“ r n”作为换行符,而 unixoid 系统只使用“ n”

Println 总是使用正确的

有几种方法可以做到这一点。我提到了两个简单的方法。

  1. 非常简单的方法如下:

    System.out.println("I\nam\na\nboy");
    
  2. It can also be done with concatenation as below:

    System.out.println("I" + '\n' + "am" + '\n' + "a" + '\n' + "boy");
    

完整的程序示例,带有一个有趣的转折:

打开一个新的空白文档并将其保存为 %yourJavaDirectory%/iAmABoy/iAmABoy.java

粘贴下面的代码并通读它。记住,我是一个初学者,所以我感谢所有的反馈!

//The class name should be the same as your Java-file and directory name.
class iAmABoy {


//Create a variable number of String-type arguments, "strs"; this is a useful line of code worth memorizing.
public static void nlSeparated(String... strs) {


//Each argument is an str that is printed.
for (String str : strs) {


System.out.println(str);


}


}


public static void main(String[] args) {


//This loop uses 'args' .  'Args' can be accessed at runtime.  The method declaration (above) uses 'str', but the method instances (as seen below) can take variables of any name in the place of 'str'.
for (String arg : args) {


nlSeparated(arg);


}


//This is a signature.  ^^
System.out.print("\nThanks, Wolfpack08!");
}


}

现在,在 Terminal/cmd 中,浏览到 %yourJavaDirectory%/iAmABoy并输入:

javac iAmABoy.java
java iAmABoy I am a boy

您可以用任何东西替换 args I am a boy

为了使代码可移植到任何系统,我将使用:

public static String newline = System.getProperty("line.separator");

这一点很重要,因为不同的操作系统使用不同的换行符: Windows 使用“ r n”,经典 Mac 使用“ r”,而 Mac 和 Linux 都使用“ n”。

评论员们——如果我说错了,请纠正我... ..。

你也可以使用 System.lineSeparator():

String x = "Hello," + System.lineSeparator() + "there";

使用像 String.format()这样的格式化程序的 %n怎么样? :

String s = String.format("I%nam%na%nboy");

正如 这个答案所说,它可以从 java 1.5获得,是到 System.getProperty("line.separator")System.lineSeparator()的另一种方式,就像这两种方式一样,是 独立操作系统

\n用于制作单独的线路;

例如:

System.out.print("I" +'\n'+ "am" +'\n'+ "a" +'\n'+ "boy");

结果:

I
am
a
boy

你可以在你的字符串中使用 <br>标记来显示在 html 页面中

分头行动。

String string = "I am a boy";
for (String part : string.split(" ")) {
System.out.println(part);
}

例子

System.out.printf("I %n am %n a %n boy");

输出

I
am
a
boy

解释

使用 %n作为独立于操作系统的新行字符比使用 \n更好,而且比使用 System.lineSeparator()更容易

为什么要使用 %n,因为在每个操作系统上,新行指的是一组不同的字符;

Unix and modern Mac's   :   LF     (\n)
Windows                 :   CR LF  (\r\n)
Older Macintosh Systems :   CR     (\r)

LF 线路传输的首字母缩写,CR回程车辆的首字母缩写。转义字符写在括号内。所以在每个操作系统上,新行代表系统特有的东西。%n与操作系统无关,它是可移植的。它代表 Unix 系统上的 \n或 Windows 系统上的 \r\n等等。因此,不要使用 \n,而是使用 %n

我用这个代码 String result = args[0].replace("\\n", "\n");

public class HelloWorld {


public static void main(String[] args) {
String result = args[0].replace("\\n", "\n");
System.out.println(result);
}
}

终端可以用 argI\\nam\\na\\boy打印出 System.out.println

I
am
a
boy

enter image description here

与站台无关的断线:

finalString = "physical" + System.lineSeparator() + "distancing";
System.out.println(finalString);

产出:

physical
distancing

备注:

  • Java 6: System.getProperty("line.separator")
  • Java7及以上版本: System.lineSeparator()

在这里! ! NewLine 被称为 CRLF(回车和换行)。

  • 对于 Linux 和 Mac,我们可以使用“ n”。
  • 对于 Windows,我们可以使用“ r n”。

样本:

System.out.println("I\r\nam\r\na\r\nboy");

结果:
output

这招对我很管用。

这里我使用的是分割函数。我把 String 从空格里拉出来。然后使用 println 函数打印出值。

    public class HelloWorld{


public static void main(String []args){
String input = "I am a boy";
String[] opuput = input.split(" ");
for (int i = 0; i < opuput.length; i++)
System.out.println(opuput[i]);
}
}