在 java 中拆分双四分逗号行

  • 本文关键字:四分 java 拆分 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

"

仅当字符串中有逗号时,它才有双引号,否则仅由逗号分隔,没有双引号。 如何正确拆分此行?我已经看到了当一切都是双重配额时如何拆分它,而不是当它只在有逗号时才完成时。

一个简单的示例解决方案可能是这样的,它负责保留双引号值中的逗号:

首先用逗号分隔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);
});
}
}

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

String str = "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";
// Replace the comma between double quotes with a replacement char you're sure isn't in the String:
// TODO: Use a more suitable character, I don't know what your text can/cannot contain
String modifiedStr = str.replaceAll("(""[^,]+),([^,]+"")", "$1🍺$2");
// Now split by comma:
String[] array = modifiedStr.split(",");
// And then change the replacement char back again to a comma:
for(int i=0; i<array.length; i++)
array[i] = array[i].replace("🍺", ",");

在线试用。

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

如果没有其他方法,您必须逐步完成。检查接下来的内容(逗号或双引号(并剪切下一个单词。

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[] {});
}

不是最好的解决方案,但它有效。

您可以按如下方式进行操作[您可以将某些部分提取改进为某些方法,但这无论如何都对您有用]

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

最新更新