简体   繁体   English

System.out.println分隔字符串中的行

[英]System.out.println to separate lines in String

I want to create cells for this spread sheet. 我想为此电子表格创建单元格。 The thing is for my toString() method, I created a variable ret and I'm adding onto it. 事情是给我的toString()方法的,我创建了一个变量ret,然后添加到它上。 The problem I'm having is how do I add a System.out.println(); 我遇到的问题是如何添加System.out.println();。 to ret? 退缩? I'm coding the spread sheet line by line using for loops but once I'm done with printing the first line, the second line that I want to print after the first line on a seperate line is being printed on the first line because I'm not sure how to ret += System.out.println(); 我使用for循环逐行对电子表格进行编码,但是一旦完成第一行的打印,就在第一行上的第一行上打印了我想在第二行上打印后要打印的第二行'不确定如何撤消+ = System.out.println();

How do i seperate the two lines? 我如何分隔这两行?

This is part of my code: 这是我的代码的一部分:

String ret = "";
String alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ret += ("             |");

for (int i = 0; i < rows; i ++) {
    ret += ("      " + alph.charAt(i) + "      |");
}

System.out.println();
ret += ("-------------+");

for (int i = 0; i < rows; i ++) {
    ret += ("-------------+");
}
String ret = "";
    String alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    ret += ("             |");
    for (int i = 0; i < rows; i ++) {
        ret += ("      " + alph.charAt(i) + "      |");
    }
   ret += "\n";
    ret += ("-------------+");
    for (int i = 0; i < rows; i ++) {
        ret += ("-------------+");
    }

Where you want the line break to be, you need to add a new line character. 您想要换行的位置,您需要添加一个新的换行符。 This may be \\n or \\r\\n depending on the operating system. 根据操作系统的不同,它可能是\\n\\r\\n You can also use System.getProperty("line.separator") to determine the correct new line character. 您还可以使用System.getProperty("line.separator")确定正确的换行符。

ret += System.getProperty("line.separator");

Add a newline character '\\n' to the string. 在字符串中添加换行符'\\n'

Replace this: 替换为:

System.out.println();

with this: 有了这个:

ret += "\n";

When it gets printed, a newline character starts a new line (hence the name). 打印后,换行符会开始换行(因此而得名)。 They're also used in text files to separate lines. 它们还用于文本文件中以分隔行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM