I have this code:
String string = "_a_b___"; String[] parts = string.split("_");
As a result, the variable parts have only three elements
parts[0] = "" parts[1] = "a" parts[2] = "b"
This is strange because there are five "_" characters, so after splitting there should be six elements, not just three of them.
I want to
parts[0] = "" parts[1] = "a" parts[2] = "b" parts[3] = "" parts[4] = "" parts[5] = ""
How to do it? Thank you very much!
From Java Documentation : -
Thus, trailing blank lines are not included in the array.
Try split("_",6)
split("_",6)
@Zakir , , , , , split("_", 6), :
split("_", 6)
String[] parts = string.split("_", string.replace("(?!_)", "").length());
.
_a_b___ [, a, b, , , ]
_a_b___
[, a, b, , , ]
Edit
@Bill F , String[] parts = string.split("_", -1); . Java doc
String[] parts = string.split("_", -1);
Source: https://habr.com/ru/post/1678507/More articles:How to use C # tuple value types in switch statement - c #Calculating the maximum length of an AES encrypted message - cRewrite algorithm to java stream with less effort? - javaCreate .md vignette with devtools - rИнкрементный решатель SMT с возможностью отбрасывания определенного ограничения - smtunrecognized option: --print-module-descriptor - javaisomorphically / universally visualized. Gaps in React 15 applications with Cloudflare HTTP proxy ("orange cloud") - reactjsAngularJS executes a function after completing another function - javascriptsite cleanup using login page - pythonImportError: no module named "_version" when importing mechanization - pythonAll Articles