Java转义字符串以存储在csv文件中
如果想将用户创建的字符串存储在 csv 文件中。 是否有一个首选库可用于转义字符串,或者我应该编写自己的函数?
If want to store user created strings in a csv file.
Is there a preferred library to use for Escaping the string or should I write my own function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于任何寻找代码的人:
将其添加到您的 pom.xml
然后使用:
UPD: 从版本 3.6 开始,
commons-lang
中的StringEscapeUtils
已弃用,因此您必须使用commons-text
相反:For anyone looking for code:
add this to your pom.xml
Then use:
UPD: as of version 3.6,
StringEscapeUtils
incommons-lang
deprecated, so you have to usecommons-text
instead:我建议您使用此处帖子推荐的库之一。虽然编写自己的 CSV 创建器/解析器似乎很容易,但您会遇到需要处理诸如包含逗号或引号的用户字符串之类的场景的问题,这有时会非常麻烦。我使用了以下库,它们工作得很好:-
I suggest you use one of the libraries recommended by the post(s) here. While it may seem easy to write your own CSV creator/parser, you are going to run into issues where you need to handle scenarios such as user strings with commas or quotes in them, which can sometimes be quite cumbersome. I used the following libraries and they worked fine:-
我最终使用了 StringEscapeUtils ,主要是因为我的项目中已经有了依赖项。效果就像一个魅力!
干杯
I ended up using StringEscapeUtils, mostly because I already had the dependency in my project. Works like a charm!
Cheers
如果您正在编写自己的实现,那么这里有 RFC 供您参考:
https://www.rfc-editor.org/rfc/rfc4180
如果你的字符串不太复杂,您可以轻松开发小功能,而不是使用某些库。我已经参考了上面的 RFC 并在我的一个项目中出于相同目的创建了小函数。
If you are writing your own implementation , then here is RFC for you reference:
https://www.rfc-editor.org/rfc/rfc4180
If your string are not too complicated you can easily develop small function rather than using some library. I have referenced above RFC and created small function for same purpose in one of my projects.