将CSV行转换为字符串数组。
// Copyright (c) 2003-present, Jodd Team (jodd.org). All Rights Reserved. import java.util.ArrayList;//时代Java 提 供 import java.util.List; public class Main{ protected static final char FIELD_SEPARATOR = ','; protected static final char FIELD_QUOTE = '"'; protected static final String DOUBLE_QUOTE = "\"\""; /** * Converts CSV line to string array. */ public static String[] toStringArray(String line) { List<String> row = new ArrayList<String>(); boolean inQuotedField = false; int fieldStart = 0; final int len = line.length(); for (int i = 0; i < len; i++) { char c = line.charAt(i); if (c == FIELD_SEPARATOR) { if (!inQuotedField) { // ignore we are quoting /* *来 自 n o w j a v a . c o m - 时 代 Java */ addField(row, line, fieldStart, i, inQuotedField); fieldStart = i + 1; } } else if (c == FIELD_QUOTE) { if (inQuotedField) { if (i + 1 == len || line.charAt(i + 1) == FIELD_SEPARATOR) { // we are already quoting - peek to see if this is the end of the field addField(row, line, fieldStart, i, inQuotedField); fieldStart = i + 2; i++; // and skip the comma inQuotedField = false; } } else if (fieldStart == i) { inQuotedField = true; // this is a beginning of a quote fieldStart++; // move field start } } } // add last field - but only if string was not empty if (len > 0 && fieldStart <= len) { addField(row, line, fieldStart, len, inQuotedField); } return row.toArray(new String[