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

5
  • Look at the javadoc especially the result for o in the example. Commented Jul 7, 2014 at 8:39
  • 1
    I would recommend just debugging split behavior in such a way: System.out.println (Arrays.asList(line.split("#"))); Commented Jul 7, 2014 at 8: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. Commented Jul 7, 2014 at 8:45
  • 1
    @Vaat666, this might be the answer to your problem: stackoverflow.com/questions/14056813/… Commented Jul 7, 2014 at 8:47
  • @user3465623 Thanks! I missed this very useful post. Commented Jul 7, 2014 at 8:51

3 Answers 3

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]);
Sign up to request clarification or add additional context in comments.

2 Comments

That's wierd. why would a limit of -1 work?
Thats how Java implemented the split method. ;) . Refer to the documentation docs.oracle.com/javase/7/docs/api/java/lang/…. "If n is non-positive then the pattern will be applied as many times as possible and the array can have any length."
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.