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 ?
Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);
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.
We use the toString() method of the list to convert the list into a string.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With