Приведем пример, если входные данные:
Код: Выделить всё
String[][] test = new String[][] {
new String[] { "0", "01", "0002"},
new String[] { "0003", "4", "5"},
};
Код: Выделить всё
0 01 0002
0003 4 5
Код: Выделить всё
\rВ приведенном выше примере вывод будет не "0\t\t01\t0002\n0003\t4\t5\n", а "0\t\t01\t0002\r\n0003\t4\t5\r\n".
Я знаю, визуально это одно и то же. Но мне нужны выходные данные этого метода, чтобы сделать что-то еще, и я подозреваю, что возврат каретки создает проблемы. У кого-нибудь есть идеи, почему эти \r растут как грибы на моих строках?
Вот мой код:
Код: Выделить всё
public class Test {
/** The maximum length of the character {@code \t}. */
public static int spaceLengthOfTab = 8;
public static void main(String[] args) {
String[][] test = new String[][] {
new String[] { "0", "01", "0002"},
new String[] { "0003", "4", "5"},
};
System.out.println(formatAndConvertToString(null, test, new StringBuffer()).toString());
String what_I_should_get = "0 01 0002\n" +
"0003 4 5\n" +
"";
String what_i_actually_get = "0 01 0002\r\n" +
"0003 4 5\r\n" +
"";
}
/** Adds to the input {@link StringBuffer} a {@link String} representing the {@code table}
* formatted in such a way to ensure all cells in a row are separated with an appropriate number
* of {@code \t} and all rows with {@code \n}.
* @param header A row that must appear above the table. Can be {@code null}, in which case the parameter will be ignored. */
public static StringBuffer formatAndConvertToString(String[] header, String[][] table, StringBuffer support) {
int[] columnMaximumWidth = new int[table[0].length];
int row, column;
// Find spaces
if( header != null )
for(column=0 ; column
Подробнее здесь: [url]https://stackoverflow.com/questions/70027337/a-problem-with-r-character-added-to-my-strings-without-being-asked-to[/url]
Мобильная версия