Home > Java > javaTutorial > body text

How to use String.split() in Java

WBOY
Release: 2023-04-18 13:19:03
forward
1760 people have browsed it

1. split(regex,limit)

The first is the split method with two parameters:

How to use String.split() in Java

Function:

Will separate the strings of the given regular expression (regex)

  • The first parameter is the separated character type passed in Symbol , such as "," etc. (can be any string)

  • The second parameter is passed in the integer limit, which represents this string Split into n parts (n here is limit).

Return value:

The array returned by this method Contains each substring of this string. These substrings end with the matched regular expression (that is, the first parameter regex entered), or by the string's Ending as an end.

Note:

  1. The substrings in the array are arranged in the order in which they appear in this string.

  2. If the input regex does not match any characters in the string, then the result array will have only one element, which is this string. (That is, if the input regex parameter does not appear in the string)

  3. If there is a positive match at the beginning of the string (that is, there are >0 regex separations at the beginning of the string) symbol), then an empty leading substring will be included at the beginning of the result array.

public class test {
    public static void main(String[] args) {
        String str = ",,1,2,3,4"; // 注意这里字符串开头就匹配到了逗号
        String[] s = str.split(",",10);// 这里先取10,后文介绍第二个参数
        for (String string : s) {
            System.out.println("子字符串"+string);
        }
        System.out.println(s.length);
    }

}
Copy after login

Running result:

There will be a before the first comma Empty substring

How to use String.split() in Java

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array . (The meaning here is that the value of limit controls the length of the result array)

How to use String.split() in Java

The above interpretation is as follows: (1) If the limit input is a positive number , then this pattern will apply limit - 1 times at most (that is, the input regex will only be used to match limit - 1 times in the string), the length of the array will not be greater than limit, and the last entry of the array will contain All inputs except the last matching delimiter (that is to say, the separated pattern is from the front to the back). Give a code for everyone to understand:

public class test {
    public static void main(String[] args) {
        String str = "1,2,3,4";
        String[] s = str.split(",",2);//这里输入limit为2,即分成2部分
        for (String string : s) {
            System.out.println("子字符串"+string);
        }
        System.out.println(s.length);
    }

}
Copy after login

Running result:

characters The string is separated into 2 substrings, and the separation pattern is

How to use String.split() in Java

from front to back (2) If the input limit is zero, the pattern will Applied as many times as possible, the resulting array can have any length, and the empty string at the end will be discarded . (That is, all regex delimiters in the string are matched), and the empty string is discarded , the code is as follows:

public class test {
    public static void main(String[] args) {
        String str = "1,2,3,4,,,";// 这里后面逗号之间的空字符串将被丢弃
        String[] s = str.split(",",0);
        for (String string : s) {
            System.out.println("子字符串"+string);
        }
        System.out.println(s.length);
    }

}
Copy after login

Running results:

The empty string at the end will not appear in the result array

How to use String.split() in Java

(3 ) If the value of input limit is negative , the pattern will be applied as many times as possible, and the array can be of any length. (The empty string at the end will not be lost)

public class test {
    public static void main(String[] args) {
        String str = ",1,2,3,4,";
        String[] s = str.split(",",-1);//limit值为负数
        for (String string : s) {
            System.out.println("子字符串"+string);
        }
        System.out.println(s.length);
    }

}
Copy after login

Running result:

The empty string at the end of the string will not be lost

How to use String.split() in Java

2. split(regex)

The next split method with only one parameter is easy, that isThe default limit value is 0.

How to use String.split() in Java

The working principle of this method is to call the two-parameter split method with the given regex parameter and a limit parameter that defaults to 0. Therefore, the trailing empty string is not included in the resulting array.

How to use String.split() in Java

The above is the detailed content of How to use String.split() in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!