-5

I have to chunk a command string into params array:

String mystr="myexe.exe param1 param2 param3";

I expect this result:

String[] params={"myexe.exe","param1","param2","param3"};

So far so good, I can simply use a string split by space char " ".

How to do it if params has space chars? How to manage double quoted params?

String mystr="myexe.exe param1 \"param2 with spaces\" param3";

I expect this result:

String[] params={"myexe.exe","param1","param2 with spaces","param3"};
Tobia
  • 9,165
  • 28
  • 114
  • 219
  • 2
    "How to do it if params has space chars? How to manage double quoted params?" => By simply writing code. Unfortunately, SO is not a "please write me the code" service. – Seelenvirtuose Oct 02 '17 at 07:53
  • 1
    Consider using a command line parsing library - no need to reinvent the wheel. There are quite a few of them. – JensS Oct 02 '17 at 07:57
  • When you try `param1 "param2 with spaces" param3` as command line arguments, what arguments did you actually get in your code? – khelwood Oct 02 '17 at 08:03
  • @khelwood this is unique string: `String cmd="param1 \"param2 with spaces\" param3";` – Tobia Oct 02 '17 at 08:07
  • @Seelenvirtuose I would like to know why your linked answer (of course it solved my problem!) is not a "please write me the code" like mine. – Tobia Oct 02 '17 at 08:11

1 Answers1

0

The java main method needs the (String[] args) parameter, so all your params or arguments are a String.

If you want to check when a param is a sequence of "Some Param" you have to create a Boolean variable, and iterate the list of params. Also you need an auxiliar Strig variable to concatenaré the arguments. So if during the iteration the param contains ", you mark the Boolean variable as true, and you iterate until you find the next param with a ".

Also you can simple underscore the word, so when you read the params you only have to do this, param.replace("_", " ");