Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a List<String> list to csv string

Tags:

I have a List of Strings. Is there a Java convenience method to convert this List to a CSV String? So "test1, test2, test3" is the result of a conversion of a List 3 String elements which contains "test1" "test2" "test3"

I could write the method myself to convert the String but maybe this is implemented by the API already ?

like image 852
blue-sky Avatar asked Nov 16 '12 10:11

blue-sky


People also ask

How do I convert a List of strings to a List of objects?

Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);

How can I convert comma separated String into a List String C#?

To convert a delimited string to a sequence of strings in C#, you can use the String. Split() method. Since the Split() method returns a string array, you can convert it into a List using the ToList() method.

Can we convert List to String in Java?

We use the toString() method of the list to convert the list into a string.

How to convert list to CSV in Python?

To convert the list to csv in Python, use the inbuilt Python CSV module or using Pandas to_csv() method or using numpy.savetxt() function.

How do you convert a list to a string in Python?

List comprehension in python generates a list of elements from an existing list. It then employs the for loop to traverse the iterable objects in an element-wise pattern. To convert a list to a string, use Python List Comprehension and the join () function.

How to create a string from a list of strings in JavaScript?

The list of strings is given as input to the join() method and It returns a string created by the elements of the list. We will create an empty string to which all the elements of the list will be concatenated. To create the string, we will take each element of the list one by one and will convert it into string.

How to create rows and cols in a CSV file?

Then we have defined two lists. The cols list defines the columns of the csv file, and rows are a list of lists that will create rows of the csv file. Use the with statement to open a shows.csv file, and if it does not exist, it will create for us and write the rows and cols to the csv file.


1 Answers

Apache Commons Lang contains a StringUtils.join() method for precisely this purpose. Note that different flavours exist.

And as of March 2014, Java 8 now has a StringJoiner

like image 63
Brian Agnew Avatar answered Oct 03 '22 16:10

Brian Agnew