Description of Java split method and repeated plit Method

Source: Internet
Author: User

Description of Java split method and repeated plit Method

I believe that the String split method is often used, but have you ever encountered the following situation:

What is the result of the following code execution?

public static void main(String[] args) {        // TODO Auto-generated method stub        String str1 = "a,b,c,,,a";        String str2 = "a,b,c,,,";        String str3 = "a,b,c, , ,";        String[] s1 = str1.split(",");        String[] s2 = str2.split(",");        String[] s3 = str3.split(",");
System. out. println ("str1 length:" + s1.length); System. out. println ("str2 length:" + s2.length); System. out. println ("str3 length:" + s3.length );
}

Execution result:

Why does this happen? I found a solution by searching for the API.

Solution:

By viewing the API, we found that the common split method is passed 0 by default. Now the solution to solve the str2 output null is to pass the second parameter as a negative number.

Public static void main (String [] args) {// TODO Auto-generated method stub String str1 = "a, B, c, a"; String str2 = ", b, c, "; String str3 =" a, B, c, "; String [] s1 = str1.split (","); string [] s2 = str2.split (",",-1); String [] s3 = str3.split (",",-1); System. out. println ("str1 length:" + s1.length); System. out. println ("str2 length:" + s2.length); System. out. println ("str3 length:" + s3.length );}

The search API finds that there are two split overload methods in the String class.

1. public String []Split(String regex)

Splits the string based on the matching of the given regular expression.

The function of this method is like using the given expression and the limit parameter 0 to call two parameters.splitMethod. Therefore, the resulting array does not contain trailing null strings.

For example"Boo: and: foo"Use these expressions to generate the following results:

Regex Result
: {"Boo", "and", "foo "}
O {"B", "", ": and: f "}

 

 
 

Parameters:
    regex-Bounded Regular Expression
Return Value:
String Array, which is determined by splitting the string based on the matching of the given regular expression.
Throw:
PatternSyntaxException-If the regular expression syntax is invalid

2. public String []Split(String regex, int limit)

Splits the string based on the given regular expression.

The array returned by this method contains the substring of this string. Each substring is terminated by another substring that matches the given expression, or ends at the end of the string. The substrings in the array are arranged in the order they appear in this string. If the expression does not match any part of the input, the resulting array has only one element, that is, this string.

LimitThe number of times that the parameter control mode applies, thus affecting the length of the obtained array. If this restrictionNIf the value is greater than 0, the mode will be applied to the maximum number of applications.N-Once, the length of the array will not be greaterNAnd the last entry of the array will contain all the inputs that exceed the last matching delimiter. IfNIf it is not positive, the pattern will be applied as many times as possible, and the array can be of any length. IfNIf the value is 0, the mode will be applied as many times as possible. The array can be of any length, and the trailing null string will be discarded.

For example"Boo: and: foo"You can use these parameters to generate the following results:

Regex Limit Result
: 2 {"Boo", "and: foo "}
: 5 {"Boo", "and", "foo "}
: -2 {"Boo", "and", "foo "}
O 5 {"B", "", ": and: f ","",""}
O -2 {"B", "", ": and: f ","",""}
O 0 {"B", "", ": and: f "} 

 

 

 

 

 

 

 

TheStr.Split (RegexN)The format is exactly the same as that produced by the following expressions:

    Pattern.compile(Regex).split(StrN)

Parameters:
regex-Bounded Regular Expression
limit-Result threshold, as described above
Return Value:
String Array, which is determined by splitting the string based on the matching of the given regular expression.
Throw:
PatternSyntaxException-If the regular expression syntax is invalid

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.