tags:

views:

72

answers:

1

I'm trying to parse arguments for a command, but if I were to put multiple spaces in a row, String.split() will leave empty Strings in the result array. Is there a way I can get rid of this?

For example: "abc 123".split(" ") results in {"abc", "", "", "", "", "123"} but what I really want is {"abc", "123"}

+5  A: 

Just use regex

"abc   123".split("\\s+");

Here \s is any whitespace character and \s+ is one or more consecutive whitespace characters.

Nikita Rybak
Thank you very much!