如何将字符串转换为集合

时间:2017-04-21 12:07:08

标签: java

我有一个以下格式的字符串。

[[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]]

我希望将每个

存储在集合中
[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]

如何拆分或存储在收藏中?

2 个答案:

答案 0 :(得分:0)

只要字符<script> function googleAL(url, title) { alert("Hello" . url, title); ga('send', 'event', { eventCategory: 'lead', eventAction: title, eventLabel: url }); } </script> 没有作为条目中值的一部分出现,这应该有效:

]

如果您知道条目中字符的转义序列(例如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; } } } ),则必须检查找到的\]索引是否代表该转义序列,以及是否从{{开始再次读取1}}索引。

答案 1 :(得分:0)

有很多方法可以做到这一点。这是我的建议:

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;
}

如果您的字符串包含[],我将其解释为空列表,即使它可能是一个元素的列表,即空字符串。如果您更喜欢后者,只需跳过for循环中的空列表检查。