Skip to content

Instantly share code, notes, and snippets.

@bcalmac
Created March 19, 2015 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcalmac/08a5d5219afa9a62811a to your computer and use it in GitHub Desktop.
Save bcalmac/08a5d5219afa9a62811a to your computer and use it in GitHub Desktop.
Java string splitting quirks
// Splitting an empty string returns an array with an empty string
assert "".split(";") == [""]
// Splitting a string consisting of just the delimiter returns an empty array
assert ";".split(";") == []
// Trailing delimiters are ignored
assert "1;;2;;".split(";") == ["1", "", "2"]
// ... but the behavior becomes consistent with a -1 extra argument
assert ";".split(";", -1) == ["", ""]
assert "1;;2;;".split(";", -1) == ["1", "", "2", "", ""]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment