简体   繁体   English

从java结果末尾删除空行

[英]Removing blank line from end of result in java

This is what my result looks like:这就是我的结果:

1.*********
2.*********
3.*********
4.*********
5.*********
6.*********
7.

I need to remove the blank line 7我需要删除空行 7

Here is my code:这是我的代码:

public static String getRectangle(int maxRows, int maxCols, char symbol) {
    new UnsupportedOperationException("You must implement this method.");
    if (maxRows<1 || maxCols<1) { 
        return null; 
    } else { 
        String result = "";
        for (int i = 0; i < maxRows; i++) { 
            for (int j = 0; j < maxCols; j++) { 
                result = result + symbol;
            } 
            result = result + "\n";
        } 
        return result;   
    }
}

Add a condition here:在此处添加条件:

if (i != maxRows - 1) {
    result = result + "\n";
}

Although i would suggest using a StringBuilder instead of a string to store the result.虽然我建议使用 StringBuilder 而不是字符串来存储结果。

You don't need any extra conditions.你不需要任何额外的条件。 Just replace return result with return result.trim()只需用return result.trim()替换return result

Since the Java version wasn't specified, as of Java 11 , you can use String::repeat with amended number of rows and columns:由于未指定 Java 版本,从Java 11 开始,您可以使用String::repeat并修改行数和列数:

public static String getRectangle(int maxRows, int maxCols, char symbol) {
    if (maxRows<1 || maxCols<1) {
        return null;
    } else {
        String row = String.valueOf(symbol).repeat(maxCols);  // create a row
        String columns = (row + "\n").repeat(maxRows - 1);    // create n-1 columns 
        return columns + row;                                 // append the last column 
                                                              // .. without the new line
    }
}

If you are stick with a lower version of Java , use StringBuilder .如果您坚持使用较低版本的 Java ,请使用StringBuilder The advantage of this class is they can be used with each other:这个类的优点是它们可以相互使用:

public static String getRectangle(int maxRows, int maxCols, char symbol) {
    if (maxRows<1 || maxCols<1) {
        return null;
    } else {
        StringBuilder row = new StringBuilder();
        StringBuilder column = new StringBuilder();
        for (int i = 0; i < maxCols; i++) {        // rows
            row.append(symbol);
        }
        for (int i = 0; i < maxRows - 1; i++) {    // n-1 columns
            column.append(row).append("\n");
        }
        return column.append(row).toString();      // append the last one w/o a new line
    }
}

Just adding a simple condition will help只需添加一个简单的条件就会有所帮助

    public static String getRectangle(int maxRows, int maxCols, char symbol) {
//        new UnsupportedOperationException("You must implement this method.");
        if (maxRows < 1 || maxCols < 1) {
            return null;
        } else {
            String result = "";
            for (int i = 0; i < maxRows; i++) {
                for (int j = 0; j < maxCols; j++) {
                    result = result + symbol;
                }
                if (i != maxRows - 1) result = result + "\n";
            }
            return result;
        }
    }

Another way to build a repeated rectangle without the trailing new line is to use combination of ancient Collections.nCopies and String.join (Java 8+):另一种在没有尾随换行的情况下构建重复矩形的方法是使用古老的Collections.nCopiesString.join (Java 8+) 的组合:

public static String getRectangle(int maxRows, int maxCols, char symbol) {
    if (maxRows<1 || maxCols<1) {
        return null;
    }
    String row = String.join("", Collection.nCopies(maxCols, String.valueOf(symbol)));
    return String.join("\n", Collections.nCopies(maxRows, row));                                
}

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

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