Skip to content

Commit 3eac15f

Browse files
authored
[CSV-211] CSVFormat.format trims last delimiter if the delimiter is a white space (#71)
* [CSV-211] fix CSVFormat.format trims last delimiter if the delimiter is a white space * [CSV-211] Add JiraCsv211Test
1 parent 57c7734 commit 3eac15f

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,9 @@ public String format(final Object... values) {
894894
final StringWriter out = new StringWriter();
895895
try (CSVPrinter csvPrinter = new CSVPrinter(out, this)) {
896896
csvPrinter.printRecord(values);
897-
return out.toString().trim();
897+
String res = out.toString();
898+
int len = recordSeparator != null ? res.length() - recordSeparator.length() : res.length();
899+
return res.substring(0, len);
898900
} catch (final IOException e) {
899901
// should not happen because a StringWriter does not do IO.
900902
throw new IllegalStateException(e);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.csv.issues;
18+
19+
import org.apache.commons.csv.CSVFormat;
20+
import org.apache.commons.csv.CSVParser;
21+
import org.apache.commons.csv.CSVRecord;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.IOException;
25+
import java.io.StringReader;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
29+
public class JiraCsv211Test {
30+
31+
@Test
32+
public void testJiraCsv211Format() throws IOException {
33+
final String[] values = new String[]{"1", "Jane Doe", "USA", ""};
34+
35+
final CSVFormat printFormat = CSVFormat.DEFAULT.withDelimiter('\t').withHeader("ID", "Name", "Country", "Age");
36+
String formatted = printFormat.format(values);
37+
assertEquals("ID\tName\tCountry\tAge\r\n1\tJane Doe\tUSA\t", formatted);
38+
39+
final CSVFormat parseFormat = CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader();
40+
CSVParser parser = parseFormat.parse(new StringReader(formatted));
41+
for (CSVRecord record : parser) {
42+
assertEquals("1", record.get(0));
43+
assertEquals("Jane Doe", record.get(1));
44+
assertEquals("USA", record.get(2));
45+
assertEquals("", record.get(3));
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)