简体   繁体   中英

How to add a separate line between .deepToString output?

Wondering how to add a separate line between .deepToString output.

Current output looks like: [[true, false, true], [false, true, false]] when it is intended to have a separate line to split each row in a separate line.

Try using .replace()

output.deepToString().replace("],", "],\n");

Thanks @Kartik for letting me know about replace() for non regex replaces

something like this should do the trick

public String array2DToString( boolean[][] array){
    StringBuilder builder = new StringBuilder( "[");
    String delimiter = "";
    for( boolean[] row : array){
        builder.append( delimiter).append( Arrays.toString( row));
        delimiter = System.lineSeparator();
    }
    builder.append( "]");
    return builder.toString();
}

StringBuilder is better way of concatenating String. since Arrays.toString works fine for single dimension we can just use that. then you can change delimiter to whatever you want to separate rows.

you can also modify the function and pass delimiter as an argument to make it more generic.

You can do something like following:

Arrays.deepToString(aComplexArray).replaceAll("], ", System.lineSeparator());

Off course, it might incorrectly start the line if the array elment contain the string "], ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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