关于Java String Manipulation [英] Regarding Java String Manipulation
问题描述
我有字符串MORET
在分割后存储在 items [1]
数组中命令。在它存储之后,我对此字符串执行replaceall并替换所有双引号。
但是我希望它存储为 MORET
。我该怎么做。在我使用split命令处理的csv文件中使用双引号重复文本字段的内容(例如:此帐户是大
一个)。所以我希望保留字符串中间的两个引号之一,如果它重复,并忽略最终引号(如果存在)。我该怎么办?
I have the string "MO""RET"
gets stored in items[1]
array after the split command. After it get's stored I do a replaceall on this string and it replaces all the double quotes.
But I want it to be stored as MO"RET
. How do i do it. In the csv file from which i process using split command Double quotes within the contents of a Text field are repeated (Example: This account is a ""large""
one"). So i want retain the one of the two quotes in the middle of string if it get's repeated and ignore the end quotes if present . How can i do it?
String items[] = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
items[1] has "MO""RET"
String recordType = items[1].replaceAll("\"","");
此记录类型后 MORET
我想要它有 MO RET
After this recordType has MORET
I want it to have MO"RET
推荐答案
不要使用正则表达式来分割CSV行。这是一个麻烦;)只是逐个字符地解析它。这是一个例子:
Don't use regex to split a CSV line. This is asking for trouble ;) Just parse it character-by-character. Here's an example:
public static List<List<String>> parseCsv(InputStream input, char separator) throws IOException {
BufferedReader reader = null;
List<List<String>> csv = new ArrayList<List<String>>();
try {
reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
for (String record; (record = reader.readLine()) != null;) {
boolean quoted = false;
StringBuilder fieldBuilder = new StringBuilder();
List<String> fields = new ArrayList<String>();
for (int i = 0; i < record.length(); i++) {
char c = record.charAt(i);
fieldBuilder.append(c);
if (c == '"') {
quoted = !quoted;
}
if ((!quoted && c == separator) || i + 1 == record.length()) {
fields.add(fieldBuilder.toString().replaceAll(separator + "$", "")
.replaceAll("^\"|\"$", "").replace("\"\"", "\"").trim());
fieldBuilder = new StringBuilder();
}
if (c == separator && i + 1 == record.length()) {
fields.add("");
}
}
csv.add(fields);
}
} finally {
if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}
return csv;
}
是的,涉及的正则表达式很少,但它只会削减结束分隔符和周围环境单个字段的引用。
Yes, there's little regex involved, but it only trims off ending separator and surrounding quotes of a single field.
您也可以抓住任何第三方 Java CSV API 。
You can however also grab any 3rd party Java CSV API.
这篇关于关于Java String Manipulation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!