用引号分割 Java 字符串
我需要在Java 中拆分字符串。分隔符是空格字符。 字符串可能包含成对的引号(内部有一些文本和空格) - 成对的引号内的整个正文应被视为单个标记。 示例:
Input: token1 "token 2" token3 Output: array of 3 elements: token1 token 2 token3
怎么做? 谢谢!
Possible Duplicate:
Can you recommend a Java library for reading (and possibly writing) CSV files?
I need to split the String in Java. The separator is the space character.
String may include the paired quotation marks (with some text and spaces inside) - the whole body inside the paired quotation marks should be considered as the single token.
Example:
Input: token1 "token 2" token3 Output: array of 3 elements: token1 token 2 token3
How to do it?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
分两次。首先是引号,然后是空格。
Split twice. First on quotes, then on spaces.
假设其他解决方案不适合您,因为它们无法正确检测匹配的引号或忽略引号文本中的空格,请尝试以下操作:
请注意,这不会处理不平衡的引号、转义的引号或其他错误/格式错误的情况输入。
Assuming that the other solutions will not work for you, because they do not properly detect matching quotes or ignore spaces within quoted text, try something like:
Note that this won't handle unbalanced quotes, escaped quotes, or other cases of erroneous/malformed input.
这是字符串标记化的简单方法
this is easy way to string tokenize