换行符字符缩进?(JAVA)

nai*_*n33 2 java string newline indentation

我试图使用\n创建一个字符串列表,但每当我添加多个字符串时,字符串缩进到前一个字符串的点,所以示例输出将是:

0 hello nain
            1 test nain
                       2 huh nain
Run Code Online (Sandbox Code Playgroud)

我不知道为什么这样做.这是我创建字符串的代码:

            String[] posts = currentTopic.getMessages("main");
            //String[] postsOutput = new String[posts.length];
            String postsOutput = "0 " + posts[0];

            for(int i = 1; i < posts.length; i++){
                postsOutput += "\n" + i + " " + posts[i];
                //postsOutput[i] = i + posts[i];
            }

            sc.close();
            return postsOutput;
Run Code Online (Sandbox Code Playgroud)

我也尝试将\n移动到附加的末尾,但结果仍然相同.任何帮助,将不胜感激.

谢谢

Pyp*_*ros 6

看起来你正处于"\n"刚下来的系统(换行)并且你错过了所需的回车.

这不应该是你应该关心的事情: line.separator属性正在适应主机操作系统,因此它的行为就像System.out.println.

  • 使用`System.getProperty("line.separator")` (2认同)