Junior Santos Junior Santos - 3 months ago 7
Java Question

How make java split java with pipe

I have to separate a string into an array that may contain empty spaces, for example,


but it is ignored when you have space at the end of the line

I am using this
regex split("\\|")

It should be like it:

but stays like this:


You can use:

String str = "|maria|joao|fernando||";
String[] tokens = str.replaceAll("^[|]|[|]$", "").split("[|]", -1));
//=> [maria, joao, fernando, ""]


  1. Replace starting and ending | using replaceAll method to get maria|joao|fernando| as input to split.
  2. Then split it using split method with 2nd parameter as -1 to return empty tokens as well.