String.split() - matching leading empty String prior to first delimiter?
Posted
by tehblanx
on Stack Overflow
See other posts from Stack Overflow
or by tehblanx
Published on 2010-04-28T19:16:24Z
Indexed on
2010/04/28
19:27 UTC
Read the original article
Hit count: 170
I need to be able to split an input String by commas, semi-colons or white-space (or a mix of the three). I would also like to treat multiple consecutive delimiters in the input as a single delimiter. Here's what I have so far:
String regex = "[,;\\s]+";
return input.split(regex);
This works, except for when the input string starts with one of the delimiter characters, in which case the first element of the result array is an empty String. I do not want my result to have empty Strings, so that something like, ",,,,ZERO; , ;;ONE ,TWO;," returns just a three element array containing the capitalized Strings.
Is there a better way to do this than stripping out any leading characters that match my reg-ex prior to invoking String.split?
Thanks in advance!
© Stack Overflow or respective owner