How do I convert the text
"[user1, 1, 2, 3, 4, 5]"
into an ArrayList? Please note that each element should be a string.
Such to say:
String str = "[user1, 1, 2, 3, 4, 5]"
ArrayList<> aryList = ...
Thanks.
How do I convert the text
"[user1, 1, 2, 3, 4, 5]"
into an ArrayList? Please note that each element should be a string.
Such to say:
String str = "[user1, 1, 2, 3, 4, 5]"
ArrayList<> aryList = ...
Thanks.
this answer hold good only in case you are have a string starting with [ and ending with ] i took an example from your question. you have many ways to delimit the string and split it to list of string. @Note : i have just wrote a test method so that you can test the code
@Test
public void test(){
String value = "[user1, 1, 2, 3, 4, 5]";
List<String> valueList = Arrays.asList(value.substring(value.indexOf('[')+1, value.lastIndexOf(']')).split(","));
System.out.println("Value list is "+valueList);
}
In pure Java, you could just remove the braces and then split the string on the comma and two spaces that you have delimiting each entry:
String text = "[user1, 1, 2, 3, 4, 5]";
// This will create a List of 6 strings:
List<String> splitList = Arrays.asList(text.replaceAll("[", "").replaceAll("]", "").split(", "));
If the entries in your array are quoted, then this syntax is just the syntax for arrays in JSON. You could use a Jackson ObjectMapper
to read the string to a list:
ObjectMapper mapper = new ObjectMapper();
String text = "[\"user1\", \"1\", \"2\", \"3\", \"4\", \"5\"]";
List<String> splitStrings = objectMapper.readValue(text, new TypeReference<List<String>>(){});