在Java中分割双quatation逗号行

时间:2018-10-25 13:45:22

标签: java split

我尝试拆分

  

“ 11020199,Abc德国,aduz,444,bieb,dc,2,2222.00,无论5dc,222.22,22.00,”“ 98,00”“,”“ 12,00”“,21-09-2018 ,06:00“

仅当字符串中有逗号时,它才使用双引号,否则,仅以逗号分隔,并且不使用双引号。 我如何正确分割这条线?我已经看到了如何在对所有内容都进行双重配额时进行拆分,而在只有逗号时才进行拆分。

4 个答案:

答案 0 :(得分:1)

一个简单的示例解决方案就是这样,它可以解决双引号保留的逗号问题:

首先用逗号分隔String,然后使用双引号将其值合并:

public class SplitAndKeepQuotedValuesCommas {

    public static void main(String[] args) {
        String source = "11020199,Abc Germany ,aduz,,444,bieb,dc,2 ,2222.00,whatever 5dc,222.22,22.00,\"\"98,00\"\",\"\"12,00\"\",21-09-2018,06:00";

        // split the String by comma
        String[] justValues = source.split(",");

        // print all items in the result
        for (String s : justValues) {
            System.out.println(s);
        }

        // prepare a List for all the values
        List<String> resultList = new ArrayList<String>();

        // then go through the values
        for (int i = 0; i < justValues.length; i++) {
            // and check if there is a String that begins with double double quotes
            if (justValues[i].startsWith("\"\"")) {
                /*
                 * if there is one, remove the double quotes from it and its successor,
                 * then concatenate them with a comma in between and add the result to the list
                 */
                String merged = justValues[i].replace("\"\"", "") + "," + justValues[i + 1].replace("\"\"", "");
                resultList.add(merged);
            /*
             * since there are still values with trailing double double quotes,
             * only add values without because they have already been added inside the merged value
             */
            } else if (!justValues[i].endsWith("\"\"")) {
                resultList.add(justValues[i]);
            }
        }

        resultList.forEach(value -> {
            System.out.println(value);
        });
    }
}

答案 1 :(得分:1)

有趣的问题。这是一个可能的解决方案(尽管我自己对此并不满意。)

filteredTextBoxExtender

Try it online.

注意:假设双双引号之间的值将只包含一个逗号。

答案 2 :(得分:0)

您可以按照以下步骤进行操作[您可以改进将某些部分提取到某种方法中,但这仍然可以为您工作]

    String[] splittedData = s.split(",");
    List<String> data = new ArrayList<>(splittedData.length);

    StringBuilder sb = new StringBuilder();
    for (String splittedDataPart : splittedData) {
        splittedDataPart = splittedDataPart.trim();
        if (sb.length() == 0 && !splittedDataPart.startsWith("\"")) {
            data.add(splittedDataPart);
            continue;
        }

        if (sb.length() != 0)
            sb.append(",");
        sb.append(splittedDataPart.replace("\"", ""));


        if (splittedDataPart.endsWith("\"")) {
            data.add(sb.toString());
            sb.setLength(0);//clearing
        }
    }

答案 3 :(得分:0)

如果没有其他效果,则必须逐步进行。检查下一个(逗号或双引号),然后切下一个单词。

public static String[] split(String s) {
    List<String> l = new ArrayList<>();

    int begin = 0;
    while (begin < s.length()) {
        int nextQuotes = s.indexOf("\"\"", begin);
        if (nextQuotes == begin) {
            l.add(s.substring(begin + 2, s.indexOf("\"\"", begin + 2)));
            begin = s.indexOf("\"\"", begin + 2) + 2;
            continue;
        }
        int nextComma = s.indexOf(',', begin);
        if (nextComma == begin) {
            l.add("");
            begin++;
            continue;
        } else if (nextComma == -1) {
            l.add(s.substring(begin));
            begin = s.length();
            continue;
        }
        l.add(s.substring(begin, nextComma));
        begin = nextComma + 1;
    }
    return l.toArray(new String[] {});
}

不是最好的解决方案,但它可以工作。