Java string split function acting weird

I notice strange behavior when using a method split()in Java.

I have a line as follows: 0|1|2|3|4|5|6|7|8|9|10

String currentString[] = br.readLine().split("\\|");
System.out.println("Length:"+currentString.length);
for(int i=0;i < currentString.length;i++){
     System.out.println(currentString[i]);
}

This will lead to the desired results:

Length: 11
0
1
2
3
4
5
6
7
8
9
10

However, if I get the line: 0|1|2|3|4|5|6|7|8||

I get the following results:

Length: 8
0
1
2
3
4
5
6
7
8

The last two empty devastations are omitted. I need empty leftovers. Not sure what I'm doing wrong. I also tried to use a split this way ....split("\\|",-1);

but returns the entire string of length 1.

Any help would be greatly appreciated!

+5
source share
6 answers

split - (- ). -1 .

UPDATE:

:

public class Test {
    public static void main(String[] args) {
    String currentString[] = "0|1|2|3|4|5|6|7|8||".split("\\|", -1);
    System.out.println("Length:"+currentString.length); 
    for(int i=0;i < currentString.length;i++){ System.out.println(currentString[i]); }
  }
}

:

Length:11
0
1
2
3
4
5
6
7
8
--- BLANK LINE --    
--- BLANK LINE --

"--- BLANK LINE -" , , . 8 | - |.

, .

+5

String.split() .

, Splitter.

.

+4

Java , :

String currentString[] = "0|1|2|3|4|5|6|7|8||".split("\\|");
System.out.println("Length:"+currentString.length); 
for(int i = 0; i < currentString.length; i++)
{
  System.out.println(currentString[i]); 
} 
+1

indexOf(), substring(). , , split().

0

IMO, , split. , :

currentString [] = br.readLine(). replace ( "||", "| |" ). split ( "\ |" ); System.out.println( ":" + currentString.length); for (int = 0; < currentString.length; ++) {  System.out.println(currentString [I]); }

, , .

0

, , , :


public class SplitTest 
{
    public static void main(String[] args)
    {
      String text = "0|1|2|3|4|5|6|7|8||";
      String pattern = "\\|";
      String [] array = text.split(pattern, -1);
          System.out.println("array length:" + array.length);
          for(int i=0; i&lt array.length; i++) 
          System.out.print(array[i]+ " ");
        } 
 }

:

array length:11
0 1 2 3 4 5 6 7 8   
0

All Articles