1

When splitting "TEST/TEST/".split("/") I will get a string array of length = 2.

How can I split getting length = 3 with elements ["TEST", "TEST", ""], thus the last element being empty string?

(I want to achieve this with native Java, and with regards to performance as the split has to take place on a few million rows one after the other).

Pshemo
  • 122,468
  • 25
  • 185
  • 269
membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

3

You can parametrize the number of desired array elements with a split overload:

String input = "TEST/TEST/";
System.out.println(Arrays.toString(input.split("/", 3)));
// or: input.split("/", -1)

Output

[TEST, TEST, ]

Notes

  • Escaping the forward slash is not necessary
  • The "performance" part of your question is a little hard to answer unless you provide more details
  • You might want to multi-thread your procedure. In Java 7, you could use the fork/join framework
  • As Pshemo points out, you can use a negative int to apply the pattern...

as many times as possible and the array can have any length.

(from API).

Community
  • 1
  • 1
Mena
  • 47,782
  • 11
  • 87
  • 106