Skip to content

Instantly share code, notes, and snippets.

@seLain
Created January 27, 2018 15:39
Show Gist options
  • Save seLain/30fc906706997fec64a1a54c0b00c893 to your computer and use it in GitHub Desktop.
Save seLain/30fc906706997fec64a1a54c0b00c893 to your computer and use it in GitHub Desktop.
prevent java.lang.String.split() from creating leading or trailing empty string
/*
example delimiters : [ !,?._'@] (including space)
*/
String[] tokens = s.replaceFirst("^[ !,?._'@]+", "").split("[ !,?._'@]+");
// to keep leading empty string
String[] tokens = s.split("[ !,?._'@]+");
// to keep trailing empty string
String[] tokens = s.replaceFirst("^[ !,?._'@]+", "").split("[ !,?._'@]+", -1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment