Java split method explained in detail

1. Problem description

Description: When writing code every day, we often encounter the need to analyze and extract the data in a string to obtain the data outside the delimiter. At this time, we have to mention the split method.

2. Method introduction

The delimiter can be any character, symbol, number, string, etc.

2.1 split(String regex)

2.1.1 Single delimiter

public class Test {
    public static void main(String[] args) {
        String str="2018,text,今天";
        //单个分隔符用引号括起来即可
        String[] data = str.split(",");
        for(int i=0;i< data.length;i++){
            System.out.println(data[i]);
        } 
    }
}

 The output of the above code is

 If the delimiter itself is "|", then you need to use the escape character "\" to have the effect, otherwise the result will be the opposite.

public class Test {
    public static void main(String[] args) {
        String str="a|bc|8";
        //java中\\表示一个普通\,\+特殊字符表示字符本身
        String[] data = str.split("\\|");
        for(int i=0;i< data.length;i++){
            System.out.println(data[i]);
        }
    }
}

 On the contrary, if used directly, it will have the opposite effect and  output a single character in the string. As follows:

public class Test {
    public static void main(String[] args) {
        String str="a|bc|8";
        //java中\\表示一个普通\,\+特殊字符表示字符本身
        String[] data = str.split("|");
        for(int i=0;i< data.length;i++){
            System.out.println(data[i]);
        }
    }
}

2.1.2 Multiple separators

public class Test {
    public static void main(String[] args) {
        String str="2021年11月18日;英语,数学,语文;";
        //多个分隔符用引号括起来,并且用“|”进行分割
        String[] data = str.split(",|;");
        for(int i=0;i< data.length;i++){
            System.out.println(data[i]);
        } 
    }
}

 2.1.2 Regular expression represents delimiter

In regular expressions, "\d+" represents one or more numbers, which is used to obtain non-numeric characters or strings from a string composed of a bunch of numbers, letters and other characters.

public class Test {
    public static void main(String[] args) {
        String str="2018年11月18日abcd85gg688";
        //正则表达式中\d+表示一个或多个数字,java中\\表示一个普通\
        String[] data = str.split("\\d+");
        for(int i=0;i< data.length;i++){
            System.out.println(data[i]);
        } 
    }
}

 The output of the above code is

 2.1.2 Special circumstances

  1. There is a delimiter at the beginning of the string: the beginning produces an empty string, the rest is normal.
  2. The delimiters are next to each other: every two delimiters produce an empty string, three delimiters result in 2 empty characters each, and so on.
  3. There is a delimiter at the end of the string: an empty string is generated at the end, and the rest is normal.

2.2 split(String regex, int limit)

  1. If limit > 0, it will be split at most n - 1 times (from left to right), the length of the array will not be greater than n, and the empty string at the end will not be discarded.
  2. If limit < 0, it will be divided as many times as there are matches, and the array can be of any length. Trailing empty strings are not discarded.
  3. If limit = 0, the array will be split as many times as there are matches, the array can be of any length, and the trailing empty string will be discarded.

That is to say, when using the split method, if you only fill in one regular expression, the trailing empty string will be discarded
 

Guess you like

Origin blog.csdn.net/qq_26893841/article/details/127919230