Commons lang StringEscapeUtils.unescapeCsv 不删除引号

Commons lang StringEscapeUtils.unescapeCsv does not remove quotes

"StringEscapeUtils.unescapeCsv" 的定义说它删除了双引号,但在我的例子中,它什么也没做。

String value = StringEscapeUtils.unescapeCsv(a[1]  );

(a1 的值 = "\"Call Us\"" 结果值为 "\"Call us\"")

不信请看调试器屏幕。这只是一个错误吗?

这不是错误。它完全按预期工作。 documentation 状态:

If the value is enclosed in double quotes, and contains a comma, newline or double quote, then quotes are removed.

这意味着在以下情况下,周围的双引号将被删除:

StringEscapeUtils.unescapeCsv("\"Call \n us!\""); // -> Call \n us!
StringEscapeUtils.unescapeCsv("\"Call, us!\"");   // -> Call, us!
StringEscapeUtils.unescapeCsv("\"Call \" us!\""); // -> Call " us!

但是在您的情况下,以下内容是正确的:

If the value is not enclosed in double quotes, or is and does not contain a comma, newline or double quote, then the String value is returned unchanged.

这意味着没有删除周围的双引号:

StringEscapeUtils.unescapeCsv("\"Call us!\""); // -> "Call us!"

而不是依赖 String.split()StringEscapeUtils.unescapeCsv() 你应该使用像 Apache Commons CSV.

这样的正确的 CSV 解析器