Java Split method using the detailed _java

Source: Internet
Author: User
Tags stub

I believe we all often use string split method, but have you encountered the following situation:

Let's think about the following code execution results

public static void Main (string[] args) {
    //TODO auto-generated the 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 results:

Why does this result, the Lookup API finds a workaround

Workaround:

By looking at the API we found that our usual split method passed by default is 0, now the solution to STR2 output null is to pass the second parameter is negative, you can

  public static void Main (string[] args) {
    //TODO auto-generated the 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 (",", -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 lookup API finds that there are two split overload methods in the String class

1.public string[] Split (String regex)

Splits this string according to the match of a given regular expression.

The method acts as if the two-parameter split method is invoked using the given expression and the constraint parameter 0来. Therefore, the resulting array does not include the trailing empty string.

For example, the string "Boo:and:foo" uses these expressions to produce the following results:

Regex Results

: {"Boo", "and", "foo"}
o {"B", "", ": And:f"}

Parameters:
Regex-Bounding regular expressions
Return:
An array of strings to split this string based on the match of a given regular expression.
Thrown:
Patternsyntaxexception-If the syntax of the regular expression is not valid

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

Splits this string according to the given regular expression.

The array returned by this method contains a substring of this string, each of which is terminated by another substring that matches the given expression, or at the end of this string. The substrings in the array are arranged in the order in which 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, the string.

The limit parameter controls the number of times the pattern is applied, thus affecting the length of the resulting array. If the limit n is greater than 0, the pattern will be applied up to n-1 times, the length of the array will not be greater than N, and the last item of the array will contain all input that exceeds the last matching delimiter. If n is not positive, then the pattern will be applied as many times as possible, and the array can be any length. If n is 0, then the pattern will be applied as many times as possible, the array can be any length, and the end empty string will be discarded.

For example, the string "Boo:and:foo" uses these parameters to produce the following results:

Regex Limit Results

: 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"} 


The Str.split (regex, N) Form that calls this method is identical to the result of the following expression:

Pattern.compile (Regex). Split (str, n)

Parameters:

Regex-Bounding regular expressions
Limit-result thresholds, as described above

Return:
An array of strings to split this string based on the match of a given regular expression.

Thrown:
Patternsyntaxexception-If the syntax of the regular expression is not valid

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.