-2

I have a string in the below format.

[[115, 1, 0123490, 63824005632, 0036760004, , 01, N, 78, , 7481067028, 
122016, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA, OH, 45805, EM, RXRELIEF CARD, 
MUCINEX DM  20 0056-32  TAB SA 12HR  600-30MG], [115, 1, 0123490, 
63824005632, 0038380001, , 
01, N, 78, , 7481067028, 122016, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA, 
OH, 45805, EM, APEX AFFINITY DISCOUNT CARD, MUCINEX DM  20 0056-32  TAB SA 
12HR  600-30MG]]

I want to store in collection with each

[115, 1, 0123490, 63824005632, 0038380001, , 01, N, 78, , 7481067028, 
122016, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA, OH, 45805, EM, APEX AFFINITY 
 DISCOUNT CARD, MUCINEX DM  20 0056-32  TAB SA 12HR  600-30MG]

[115, 1, 0123490, 63824005632, 0036760004, , 01, N, 78, , 7481067028, 
122016, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA, OH, 45805, EM, RXRELIEF CARD, 
MUCINEX DM  20 0056-32  TAB SA 12HR  600-30MG]

How can I split or store in collection?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
asritha sanka
  • 11
  • 1
  • 3
  • 2
    What have you tried so far? Show us some code. If you haven't tried anything yet, have a look at `String#split`. – Robin Topper Apr 21 '17 at 12:08
  • Possible duplicate of [convert ArrayList.toString() back to ArrayList in one call](http://stackoverflow.com/questions/2774142/convert-arraylist-tostring-back-to-arraylist-in-one-call) – Tom Apr 21 '17 at 12:09
  • It’s not a precise duplicate since this question is about nested lists/arrays in the string, the other one isn’t. – Ole V.V. Apr 21 '17 at 12:25
  • What is your desired type of the result? `List>`? – Ole V.V. Apr 21 '17 at 12:31

2 Answers2

0

This should work as long the character ] does not appear as part of a value inside an entry:

public static void main(String[] args) {
    String clob = "[[115, 1, 0123490, 63824005632, 0036760004, , 01, N, 78, , 7481067028, 122016, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA, OH, 45805, EM, RXRELIEF CARD, MUCINEX DM 20 0056-32 TAB SA 12HR 600-30MG], [115, 1, 0123490, 63824005632, 0038380001, , 01, N, 78, , 7481067028, 122016, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TABORA, EMMANUEL, J, 4732 WENATCHIE TRL, LIMA, OH, 45805, EM, APEX AFFINITY DISCOUNT CARD, MUCINEX DM 20 0056-32 TAB SA 12HR 600-30MG]]";
    List<String> entries = new ArrayList<>();
    int start = 1;
    while (true) {
        start = clob.indexOf("[", start);
        int end = clob.indexOf("]", start);
        if (start != -1 && end != -1) {
            entries.add(clob.substring(start, end + 1));
            start = end + 1;
        } else {
            break;
        }
    }
}

If you know the escape sequence for characters inside your entries (e.g. \]) you have to check if the found end index represents that escape sequence and if, read again, starting from the end index.

Harmlezz
  • 7,972
  • 27
  • 35
0

There are many ways to do it. Here’s my suggestion:

public static List<List<String>> stringTo2DList(String input) {
    if (input.equals("[]")) {
        return Collections.emptyList();
    }
    if (! input.startsWith("[[")) {
        throw new IllegalArgumentException("Not a list of lists");
    }
    if (! input.endsWith("]]")) {
        throw new IllegalArgumentException("Not a list of lists");
    }
    List<List<String>> result = new ArrayList<>();
    String[] innerLists = input.substring(2, input.length() - 2).split("\\], \\[");
    for (String innerList : innerLists) {
        // check for empty inner list
        if (innerList.isEmpty()) {
            result.add(Collections.emptyList());
        } else {
            result.add(Arrays.asList(innerList.split(", ")));
        }
    }
    return result;
}

Should your string contain [], I am interpreting it as an empty list even though it might be a list of one element, the empty string. If you prefer the latter, just skip the check for the empty list in for loop.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161