The difference between .split(",", -1) and .split(",")

The difference between .split(",", -1) and .split(",")

.split(",", -1)And .split(",")the difference is:

String a="河南省,,金水区";

.split(",")

a.split(",") = [河南省,金水区 ]

.split(",", -1)

And a.split(",",-1) = [河南省, ,金水区 ]
i.e. .split ( ",", -1) ; null value will be saved.


String a="a,b,c," ;

When a.split(”,”)you get an array when it is: [a,b,c],

And the a.split(",",-1)resulting array is:, [a,b,c, ]when using .split(",", -1); null values ​​will be saved.


1. When the last digit of the string has a value, there is no difference between the two

2. When the last digit or N digit of the string is a separator, the former will not continue to be split, while the latter will continue to split. That is, the former does not retain the null value, and the latter is retained.

For example:

package stringsplit;

public class stringSplit {
    
    

     public static void main(String[] args) {
    
    
          String line = "hello,,world,,,";
          String res1[] = line.split(",");
          String res2[] = line.split(",", -1);

          int i=1;
          for(String r: res1)
              System.out.println(i+++r);

          System.out.println("========================");

          i=1;
          for(String r: res2)
              System.out.println(i+++r);
     }

}

The output result is:
1hello
2
3world

========================

1hello
2
3world
4
5
6

Guess you like

Origin blog.csdn.net/qq_32727095/article/details/113862986