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
  • 299
  • 1
  • 13
  • This behaviour is defined in the documentation, see https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String) – Progman Oct 21 '21 at 18:11
  • 1
    Ok, but that does not make it correct. How do I make split() return ALL parts between the delimiters? – Big Guy Oct 21 '21 at 18:13
  • Use the `split()` method which accepts two arguments, see https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String,%20int) – Progman Oct 21 '21 at 18:14
  • If that is regex then use this `\\.+` – MMD Oct 21 '21 at 18:16
  • @MMD that won't help here. maybe you misunderstood the problem? – eis Oct 21 '21 at 18:20
  • @Progman - your comment should be an answer. `split("\\.",-1)` does it – Sam Oct 21 '21 at 18:22
  • 6
    Does this answer your question? [Java String split removed empty values](https://stackoverflow.com/questions/14602062/java-string-split-removed-empty-values) – lkatiforis Oct 21 '21 at 18:58

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
  • 49,147
  • 13
  • 140
  • 191
  • 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