3

In Java:

String base = "a|a||";
String[] stri= .split("\\|");

produces a string array with length 2.

On the other hand in python:

base = "a|a||"
base.split("|")

produces an array with length 4. What do I have to do to get the same result in Java?

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
Christian
  • 25,249
  • 40
  • 134
  • 225

1 Answers1

4

Use split with limit set to negative value:

String base = "a|a||";
String[] stri= .split("\\|", -1);

From the docs (the number at the and is n):

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115