1

I am using split() to gather values between periods in a String. If the user enters trailing periods, then split() does not return those periods as empty strings, but drops them:

String[] parts = "a.b.c...d...e".split("\\.");
   for ( int i = 0; i < parts.length; i++ )
       System.out.println("" + i + ": '" + parts[i] + "'" );

0: 'a'
1: 'b'
2: 'c'
3: ''
4: ''
5: 'd'
6: ''
7: ''
8: 'e'

Removing the last 'e':

String[] parts = "a.b.c...d...".split("\\.");
   for ( int i = 0; i < parts.length; i++ )
       System.out.println("" + i + ": '" + parts[i] + "'" );


0: 'a'
1: 'b'
2: 'c'
3: ''
4: ''
5: 'd'

What happened to parts 6 and 7? It seems that split() simply dropped them. Yet they are there in the String. Should not split() return parts 6 and 7 as empty strings?

Big Guy
  • 332
  • 1
  • 13

1 Answers1

6

See 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.

So it is behaving as defined. If you're not happy with that, you can do what the manual suggests and use a negative parameter for limit.

String[] parts = "a.b.c...d...".split("\\.", -1);
for ( int i = 0; i < parts.length; i++ )
   System.out.println("" + i + ": '" + parts[i] + "'" );

0: 'a'
1: 'b'
2: 'c'
3: ''
4: ''
5: 'd'
6: ''
7: ''
8: ''
eis
  • 51,991
  • 13
  • 150
  • 199
  • And there you go. I learned Java over 20 years ago, before split() used regex. I really should find a refresher course... – Big Guy Oct 21 '21 at 18:21
  • 1
    @BigGuy AFAIK java split() has always used regex. IIRC it was introduced with java 1.4, back when regular expressions were introduced to the platform. – eis Oct 21 '21 at 18:26
  • Hmmm, I remember using ```.split(".")``` to break apart strings. Which of course fails in regex. Just looked at some really old code, and and it uses ```.split(".")```, and it did work at that time – Big Guy Oct 21 '21 at 18:30
  • @BigGuy just re-checked the javadocs out of curiosity and split() was indeed introduced with java 1.4, with regex, and it didn't exist until then. – eis Oct 22 '21 at 06:54