A
A
Arthur2016-02-01 16:09:32
Java
Arthur, 2016-02-01 16:09:32

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);

output - length 8
Although the string is larger than the characters ";" also more.
split cuts off everything after the word "word"
If I move the word further, the length of the array will increase
{
  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);
  }

At the output, the length of the array is 16.
Please tell me what's wrong, or indicate the direction for the proceedings.
Thank you very much.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Oparin, 2016-02-01
@antoart

This is "normal behavior" - empty end pieces are discarded.
If you need everything - use
s.split(";", -1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question