Tuesday, December 28, 2010

Splitting a string using the empty string as the delimiter yields leading empty string but no trailing empty string

Programmer Question

Suppose you have this expression in Java:



"adam".split("")


This is telling Java to split "adam" using the empty string ("") as the delimiter. This yields:



["", "a", "d", "a", "m"]


Why does Java include an empty string at the start, but not at the end? Using this logic, shouldn't the result have been:



["", "a", "d", "a", "m", ""]


Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails