I have a String value as – [“My, fancy, text”, “My, other, value”]
I want to get the output as a List with elements as –
My, fancy, text My, other, value
The enclosing bracketts are to be removed.It may happen that enclosing bracketts ‘[‘ and ‘]’ are not present. ‘
Advertisement
Answer
Assuming the requirement is to remove all the square brackets and finding text between double quotes as individual string. Below Code might help
String[] data = Stream.of( input.replaceAll("\[", "")//replace all [ .replaceAll("\]","")//replace all ] .replaceAll("",","")// replace the comma between strings, .split("""))//now split the string based on double quotes .filter(str ->str.trim().length() > 1)//filtering out the string which contains only whitespace .toArray(String[]::new);//collecting the data in an array //data[0] = My, fancy, text //data[1] = My, other, value