Java splits strings into arrays
•
Java
See English answers > java string split removed empty values 6
String values = "0|0|0|1|||0|1|0|||";
I need to put the values into an array There are 3 possible strings: "0", "1" and ""
My problem is, when I try to use split ():
String[] array = values.split("\\|");
My value is only saved to the last 0 It looks like the "|||" part is trimmed What on earth did I do wrong?
thank you
Solution
This behavior is in string Clear record in split (string regex) (emphasize my):
If you want to include those trailing empty strings, you need to use string Negative values of split (string regex, int limit) and the second parameter (limit):
String[] array = values.split("\\|",-1);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码