Reputation: 5219
So I'm trying to split a string
String[] tokens = coded_string.split("\\)|\\(|\\,|\\s+");
so if the coded string is students = load(classlist.csv)
all tokens are fine
token[0] = "students"
token[1] = "="
token[2] = "load"
token[3] = "classlist.csv"
but when the string students = load ( classlist.csv )
spaces between brackets i get empty tokens why is that and how can i fix it?
token[0] = "students"
token[1] = "="
token[2] = "load"
token[3] = ""
Upvotes: 1
Views: 107
Reputation: 47954
Because you have two delimiters in a row, an empty space followed by an open paren, it returns a match on the 'nothing' that is between them as an empty string. You could use a character class instead of alternation to match entire blocks of potential delimiter characters. You haven't stated your actual requirements, so it's tough to know if that would be strictly correct for all inputs.
String[] tokens = coded_string.split("[)(,\\s]+");
Upvotes: 3