How to prevent java.lang.String.split() from creating a leading empty string?-ThrowExceptions

Exception or error:

passing 0 as a limit argument prevents trailing empty strings, but how does one prevent leading empty strings?

for instance

String[] test = "/Test/Stuff".split("/");

results in an array with “”, “Test”, “Stuff”.

Yeah, I know I could roll my own Tokenizer… but the API docs for StringTokenizer say

“StringTokenizer is a legacy class that is retained for compatibility
reasons although its use is discouraged in new code. It is recommended
that anyone seeking this functionality use the split”

How to solve:

Your best bet is probably just to strip out any leading delimiter:

String input = "/Test/Stuff";
String[] test = input.replaceFirst("^/", "").split("/");

You can make it more generic by putting it in a method:

public String[] mySplit(final String input, final String delim)
    return input.replaceFirst("^" + delim, "").split(delim);

String[] test = mySplit("/Test/Stuff", "/");


Apache Commons has a utility method for exactly this: org.apache.commons.lang.StringUtils.split


Actually in our company we now prefer using this method for splitting in all our projects.


I don’t think there is a way you could do this with the built-in split method. So you have two options:

1) Make your own split

2) Iterate through the array after calling split and remove empty elements

If you make your own split you can just combine these two options

public List<String> split(String inString)
   List<String> outList = new ArrayList<>();
   String[]     test    = inString.split("/");

   for(String s : test)
       if(s != null && s.length() > 0)

   return outList;

or you could just check for the delimiter being in the first position before you call split and ignore the first character if it does:

String   delimiter       = "/";
String   delimitedString = "/Test/Stuff";
String[] test;

    //start at the 1st character not the 0th
    test = delimitedString.substring(1).split(delimiter); 
    test = delimitedString.split(delimiter);


I think you shall have to manually remove the first empty string. A simple way to do that is this –

  String string, subString;
  int index;
  String[] test;

  string = "/Test/Stuff";
  index  = string.indexOf("/");
  subString = string.substring(index+1);

  test = subString.split("/"); 

This will exclude the leading empty string.


I think there is no built-in function to remove blank string in Java. You can eliminate blank deleting string but it may lead to error. For safe you can do this by writing small piece of code as follow:

  List<String> list = new ArrayList<String>();

  for(String str : test) 
     if(str != null && str.length() > 0) 

  test = stringList.toArray(new String[list.size()]);


You can use StringTokenizer for this purpose…

String test1 = "/Test/Stuff";
        StringTokenizer st = new StringTokenizer(test1,"/");


This is how I’ve gotten around this problem. I take the string, call .toCharArray() on it to split it into an array of chars, and then loop through that array and add it to my String list (wrapping each char with String.valueOf). I imagine there’s some performance tradeoff but it seems like a readable solution. Hope this helps!

 char[] stringChars = string.toCharArray(); 
 List<String> stringList = new ArrayList<>(); 

 for (char stringChar : stringChars) { 


You can only add statement like if(StringUtils.isEmpty(string)) continue; before print the string. My JDK version 1.8, no Blank will be printed.

Leave a Reply

Your email address will not be published. Required fields are marked *