With:
"(something)".split("\\W+")
it's assuming the delimiter comes between fields, so what you end up with is:
"" "something" "" <- fields
( ) <- delimiters
You could fix that by trimming the string first to remove any leading or trailing delimiters, something like:
"(something)".replaceAll("^\\W*","").replaceAll("\\W*$","").split("\\W+")
With something like:
"boo:and:foo".split("o", 0)
you'll get:
"b" "" ":and:f" <- fields
o o <- delimiters
because you have consecutive delimiters (which don't exists when the delimiter is ":"
) which are deemed therefore to have an empty field between them.
And the reason you don't have trailing blank fields because of foo
at the end, has to do with that limit of zero. In that case, trailing (not leading) empty fields are removed.
If you want to also get rid of the empty fields in the middle, you can instead use "o+"
as the delimiter since that will greedily absorb consective o
characters into a single delimiter. You can also use the replaceAll
trick shown above to get rid of leading empty fields.