Rishabh
Rishabh

Reputation: 99

Include comma in a field value of csv (delimeter as comma) in java

I want to create a csv file in which delimiter is comma seperated.While csv is read it make s5 string in comma seperation. Want to keep s5 string in single column.

   String s1="quick";
   String s2="brown";
   String s3="fox";
   String s4="jump";
   String s5="over,the,lazy,dog";


  String csvRecord = String.format("%s,%s,%s,%s,%s", s1,s2,s3,s4,s5);

Can anybody help me out, as i am new to strings.

Upvotes: 1

Views: 4993

Answers (4)

Jeronimo Backes
Jeronimo Backes

Reputation: 6289

Unless you are toying with some assignment you absolutely NEED a CSV library to handle this for you. You also need to care about line endings and other quotes inside the values, not only the delimiter

Try the following code with univocity-parsers:

CsvWriterSettings settings = new CsvWriterSettings();
CsvWriter writer = new CsvWriter(settings);
String result = writer.writeRowToString(s1,s2,s3,s4,s5);

Hope this helps.

Disclaimer: I'm the author of this library. It's open source and free (Apache 2.0 license)

Upvotes: 0

Danny_ds
Danny_ds

Reputation: 11406

To include a delimiter in the data, you have to enclose the field with double quotes in the resulting csv:

quick,brown,fox,jump,"over,the,lazy,dog"

If there are double quotes inside some fields those have to be escaped:

123,"24"" monitor, Samsung",456  // Windows style
123,"24\" monitor",456           // Linux style

You don't have to quote all the fields, only those containing a delimiter or a double quote (or a newline).

See RFC4180 for more details on the csv common format.

Upvotes: 0

Hearen
Hearen

Reputation: 7838

Just as other answers mentioned, you have to actually consider many special cases and the most typical two special cases will be: comma and double quotes.

As Chewtoy enclosed, the CSV wiki has clearly specify the format as:

enter image description here

Year,Make,Model,Description,Price
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00

To handle these two cases, you can try something as:

public class CsvAddComma {
    public static void main(String... args) {
        List<String> list = new ArrayList<>();
        list.add("quick");
        list.add("over,the,lazy,dog");
        list.add("Venture \"Extended Edition, Very Large\"");
        list.stream()
                .map(s -> convertToCsvFormat(s))
                .forEach(System.out::println);
    }

    private static String convertToCsvFormat(String input) {
        if (input.contains("\"")) {
            input = input.replaceAll("\"", "\"\"");
        }
        if (input.contains(",")) {
            input = String.format("\"%s\"", input);
        }
        return input;
    }
}

And the output:

quick
"over,the,lazy,dog"
"Venture ""Extended Edition, Very Large"""

As for other special cases, you need to handle them according to your current case. Perhaps they can be ignored, perhaps not.

Upvotes: 0

Idva
Idva

Reputation: 194

I think you need to add them in a double string.

Try this:

String s5="\"over,the,lazy,dog\"";

Upvotes: 2

Related Questions