Possible Duplicate:
Java split() method strips empty strings at the end?
In Java, I’m using the String split method to split a string containing values separated by semicolons.
Currently, I have the following line that works in 99% of all cases.
String[] fields = optionsTxt.split(";");
When using following String everything is perfect:
"House;Car;Street;Place" => [House] [Car] [Street] [Place]
But when i use following String, split Method ignores the last two semicolons.
"House;Car;;" => [House][Car]
What’s wrong? Or is there any workaround?
Try below:
Refer to Javadoc for the split method taking two arguments for details.
When calling
String.split(String), it callsString.split(String, 0)and that discards trailing empty strings (as the docs say it), when callingString.split(String, n)withn < 0it won’t discard anything.