0

This is perhaps a duplicate question, but I didn't find anything that would be helpful for me.

Look, I have a string (actually that's command string) that is looking like command arg1 "arg2 with whitespaces but enclosed with quotes".

So I need to parse it, construct an array or arguments - something like {"arg1", "arg2 with whitespaces but enclosed with quotes"}

There's no way to do so with a simple split - quote-enclosed argument may be with spaces so split will consider them "arguments" too.

Any libs, or something else helpful relate to this?

KeterDev
  • 7
  • 1
  • 6
  • see this https://stackoverflow.com/questions/7804335/split-string-on-spaces-in-java-except-if-between-quotes-i-e-treat-hello-wor?noredirect=1&lq=1 – dumbPotato21 Oct 20 '17 at 15:54
  • Check my edited answer – Sergio Oct 20 '17 at 16:07
  • *"Any libs, or something else helpful relate to this?"* [Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.](http://meta.stackoverflow.com/questions/254393) –  Oct 20 '17 at 16:12
  • Welcome to Stack Overflow! Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Oct 20 '17 at 16:13

1 Answers1

0

If you have only two args you can use :

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

if you have more see the Q&A in the comments.

Eritrean
  • 15,851
  • 3
  • 22
  • 28