5

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"}

user322652
  • 85
  • 1
  • 4

1 Answers1

16

Just use regex

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

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

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181