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"}
Just use regex
"abc 123".split("\\s+");
Here \s
is any whitespace character and \s+
is one or more consecutive whitespace characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With