如何在 Java 中打印添加换行符的字符串?

问题描述 投票:0回答:17

我有一个像这样的字符串

"I am a boy".

我想这样打印

"I 
am 
a
boy".

有人能帮帮我吗?

java string newline
17个回答
157
投票
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

144
投票

您也可以使用

System.lineSeparator()

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

60
投票

例子

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)

LFLine Feed 的缩写,CRCarriage Return 的缩写。转义字符写在括号内。所以在每个操作系统上,新行代表系统特定的东西。

%n
与操作系统无关,它是可移植的。它在 Unix 系统上代表
\n
或在 Windows 系统上代表
\r\n
等等。因此,不要使用
\n
,而是使用
%n


30
投票

可以通过多种方式完成。我提到了 2 种简单的方法。

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

    System.out.println("I\nam\na\nboy");
    
  2. 也可以通过连接来完成,如下所示:

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

15
投票

尝试:

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

13
投票

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

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

这很重要,因为不同的操作系统对换行符使用不同的表示法:Windows 使用“ ", Classic Mac 使用 " ",而 Mac 和 Linux 都使用 " “.

评论员 - 如果我在这方面有误,请纠正我......


8
投票

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

例子:

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

结果:

I
am
a
boy

7
投票

独立于平台的换行符:

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

输出:

physical
distancing

备注:

  • Java 6:
    System.getProperty("line.separator")
  • Java 7 及更高版本:
    System.lineSeparator()

6
投票

如果您只想在控制台中打印换行符,您可以使用

\n
换行符。

如果您想在 Swing 组件中打断文本,您可以使用 HTML 及其

<br>

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

5
投票

如果你想让你的代码不特定于操作系统,你应该为每个单词使用 println

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

因为 Windows 使用“ “因为换行符和 unixoid 系统只使用” “

println 总是使用正确的


4
投票

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

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

正如this answer所说,它可从java 1.5获得,是

System.getProperty("line.separator")
System.lineSeparator()
的另一种方式,并且像这两个一样,是OS独立


1
投票

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

打开一个新的空白文档并将其另存为

%yourJavaDirectory%/iAmABoy/iAmABoy.java
。 “iAmABoy”是班级名称。

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

//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!");
    } 

}

现在,在终端/cmd 中,浏览到

%yourJavaDirectory%/iAmABoy
并输入:

javac iAmABoy.java
java iAmABoy I am a boy

您可以用任何东西替换 args

I am a boy


1
投票

去分裂。

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

0
投票

我用这个代码

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);
    }
}

使用终端我可以使用 arg

I\\nam\\na\\boy
制作
System.out.println
打印出来

I
am
a
boy


0
投票

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

  • 对于 Linux 和 Mac,我们可以使用“ “.
  • 对于Windows,我们可以使用“ “.

样品:

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

结果:

对我有用。


-1
投票

您可以在字符串中使用

<br>
标签以在 html 页面中显示


-1
投票

这里我用的是split函数。我从空格中制动了字符串。然后我使用 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]);
         }        
}
© www.soinside.com 2019 - 2024. All rights reserved.