【问题标题】:Parse command line-like line [duplicate]解析类似命令行的行[重复]
【发布时间】:2017-10-20 15:37:45
【问题描述】:

这可能是一个重复的问题,但我没有找到任何对我有帮助的东西。

看,我有一个字符串(实际上是命令字符串),看起来像 command arg1 "arg2 with whitespaces but enclosed with quotes".

所以我需要解析它,构造一个数组或参数——比如{"arg1", "arg2 with whitespaces but enclosed with quotes"}

使用简单的拆分无法做到这一点 - 引号括起来的参数可能带有空格,因此拆分也会将它们视为“参数”。

任何库或其他有用的东西与此相关?

【问题讨论】:

标签: java string


【解决方案1】:

如果你只有两个参数,你可以使用:

    String myString = "command arg1 \"arg2 with whitespaces but enclosed with quotes\"";
    String [] splited = myString.split(" ",3);
    System.out.println(Arrays.toString(splited));

如果您有更多,请参阅 cmets 中的问答。

【讨论】:

    猜你喜欢
    • 2015-02-28
    • 2015-09-20
    • 1970-01-01
    • 2020-01-01
    • 2010-10-19
    • 2013-08-04
    • 2016-09-25
    • 2017-05-16
    • 2011-03-20
    相关资源
    最近更新 更多