Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<String> to string back to List in java

Tags:

java

arraylist

I have stored a java.util.List of Strings such as

[data1,data2,data3,data3,data7]

in a String.

How can I convert it back to a List of String, knowing that some values are just empty ?

Update: I am not able to convert implicitly. Getting error cannot cast string to string[]. Also tried converting to list. No success.

Update2: , cannot be used to split because the data also contains ,

Update 3: [/Simplify.do, action, temp,test,data] Data with , but without spaces is a single data.


1 Answers

If I am understanding the questions correctly, and your data has a delimiter of a comma.

//if you have the brackets remaining and don't want them, remove them
data = data.replace("[","").replace("]","");


String data = "data1,data2,data3";
//You would put the second parameter as -1 if you want to keep any trailing blank values 
List<String> smapleData= Arrays.asList(data.split(",",-1));
//or if you don't want to keep trailing blanks
List<String> sampleDataTwo = Arrays.asList(data.split(","));

that would bring it back to a list of strings.

like image 61
DejaVuSansMono Avatar answered Apr 01 '26 10:04

DejaVuSansMono



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!