4

I have come across an unexpected feature in the split function of String in Java, here is my code:

final String line = "####";
final String[] lineData = line.split("#");
System.out.println("data: " + lineData[0] + " -- " + lineData[1]);

This code gives me an ArrayIndexOutOfBoundsException, whereas I would expect it to print "" and "" (two empty Strings), or maybe null and null (two null Strings).

If I change my code for

final String line = " # # # #";
final String[] lineData = line.split("#");
System.out.println("data: " + lineData[0] + " -- " + lineData[1]);

Then it prints " " and " " (the expected behaviour).

How can I make my first code not throwing an exception, and giving me an array of empty Strings?

Thanks

gprathour
  • 14,813
  • 5
  • 66
  • 90
toni07
  • 356
  • 7
  • 20
  • Look at the [javadoc](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-) especially the result for o in the example. – Raghuram Jul 07 '14 at 08:39
  • 1
    I would recommend just debugging `split` behavior in such a way: `System.out.println (Arrays.asList(line.split("#")));` – Dmitry Ginzburg Jul 07 '14 at 08:39
  • @Raghuram, thanks, I saw this example. But don't you think this is a dangerous implementation of split? In PHP, with the explode function you get as many empty strings as you would expect (no trolling here, I like both PHP and Java). I say dangerous because if you are parsing a CSV file header with empty column, you simply miss it. – toni07 Jul 07 '14 at 08:45
  • 1
    @Vaat666, this might be the answer to your problem: http://stackoverflow.com/questions/14056813/behaviour-of-string-split-in-java-1-6 – luiscosta Jul 07 '14 at 08:47
  • @user3465623 Thanks! I missed this very useful post. – toni07 Jul 07 '14 at 08:51

3 Answers3

8

You can use the limit attribute of split method to achieve this. Try

final String line = "####";
final String[] lineData = line.split("#", -1);
System.out.println("Array length : " + lineData.length);
System.out.println("data: " + lineData[0] + " -- " + lineData[1]);
Syam S
  • 8,421
  • 1
  • 26
  • 36
  • That's wierd. why would a limit of -1 work? – faizal Jul 07 '14 at 16:27
  • Thats how Java implemented the split method. ;) . Refer to the documentation http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29. `"If n is non-positive then the pattern will be applied as many times as possible and the array can have any length."` – Syam S Jul 07 '14 at 16:33
1

As always, answer is written in the Javadoc

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

Since your array is composed only by empty strings, they are not added to it, thus trying to access the values result in an ArrayOutOfBoundException.

Narmer
  • 1,414
  • 7
  • 16
0

If I understand your question, this would do it -

final String line = " # ";
final String[] lineData = line.split("#");
System.out.println("data: " + lineData[0] + " -- " + lineData[1]);

The problem is that the empty string isn't a character.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249