15

Suppose I have an array of int, float, string etc. Is there any utility API (e.g. Commons, Guava) that will give me a comma separated string?

Like so,

int[] a = {1,2,3,4,5}. 
String s = magicAPI.getCSV(a); // s == "1,2,3,4,5";
4
  • Arrays.toString(a).substring(1).replaceAll("]$", "") // :P Commented Jul 18, 2012 at 16:28
  • @rascio Arrays#toString also inserts spaces after each comma, which you can't remove easily with a regex as it might be part of a String (in the case of a String[]). Commented Jul 18, 2012 at 16:31
  • @assylias Although in that case you probably need to handle the case of the delimiter being part of the string more generally. (By using a full CSV library for instance. Commented Jul 18, 2012 at 16:34
  • it was a joke it's the most stupid way I found to do it...it's plenty of better way to do it :) Commented Jul 18, 2012 at 16:34

6 Answers 6

33

For this simple use case, you can simply join the strings with comma. If you use Java 8:

String csv = String.join(",", yourArray);

otherwise commons-lang has a join() method:

String csv = org.apache.commons.lang3.StringUtils.join(yourArray, ",");
Sign up to request clarification or add additional context in comments.

1 Comment

getting The method join(CharSequence, CharSequence...) in the type String is not applicable for the arguments (String, int[]) what do I miss?
6

You mention Google Guava, which has the Joiner utility for this:

String s = Joiner.on(",").join(a);

Comments

4

I've used OpenCSV in the past.

StringWriter stringWriter = new StringWriter();
int[] a = {1,2,3,4,5};
String[] b = new String[a.length];
for ( int i = 0; i < a.length; i++) {
    b[i] = a[i];
}
CSVWriter csvWriter = new CSVWriter(stringWriter, ",");
csvWriter.writeNext(b);

However, for such a trivial example you might want to just use the a StringBuilder and a for loop

2 Comments

Doesn't the writeNext() method NOT require you to insert commas yourself ? Otherwise what's the point ?
Is there a vectorized version of this?
3

After digging more, I found http://static.springsource.org/spring/docs/1.1.5/api/org/springframework/util/StringUtils.html StringUtils API in Spring that can do it. Since , I'm already using Spring, I guess I will stick with it.

Comments

1

Commons Apache CSV appears to consolidate 3 other CSV libraries, although I can't find a release post-2007.

A quick look suggests OpenCSV will do what you want via a CSVWriter.

 CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
 // feed in your array (or convert your data to an array)
 String[] entries = "first#second#third".split("#");

Comments

0

I have created a new method to do thisinstead of going for openCSV

public String convertArrayToCsv(String[] IncomingArray)
{
    StringBuilder sb=new StringBuilder();
    for (int i=0;i<IncomingArray.length;i++)
    {
        sb=sb.append(IncomingArray[i]);
        if(i != IncomingArray.length-1)
        {
            sb.append(",");
        }
    }
    return sb.toString();
}//end of convertArrayToCsv method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.