Answer the question
In order to leave comments, you need to log in
Why does the split(regex) method in Java discard the desired part of the string?
Hello!
Please help me understand what's going on.
There is a string from a .csv file.
There is a word in a cell, followed by several empty cells. I wanted to split the string into parts and get ALL the cells in the array.
I use split(regex), but part of the string is lost.
String s = ";;;;;;;word;;;;;;;;";
String[] ss = s.split(";");
System.out.println(ss.length);
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
String s = ";;;;;;;;;;;;;;;word";
String[] ss = s.split(";");
System.out.println(ss.length);
}
Answer the question
In order to leave comments, you need to log in
This is "normal behavior" - empty end pieces are discarded.
If you need everything - uses.split(";", -1)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question