Some confusions about the split (string RegEx) method of the string class in Java

Source: Internet
Author: User

 Some confusions about the split (string RegEx) method of the string class in Java

This class is described in the JDK Chinese API as follows:

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 result array does not contain trailing null strings.

For example"Boo: And: foo"Produce results with the following expressions:

RegEx

Result

:

{"Boo", "and", "foo "}

O

{"B", "", ": And: F "}

 

 

Parameters:
RegEx-Bounded Regular Expression
Return Value:
String Array, which is split by matching the given regular expression to generate this array.
Throw:
Patternsyntaxexception-If the regular expression syntax is invalid
Start with the following versions:
1.4
For more information, see:
Pattern

 

CodeAs follows:

String longstring = "AA: BB: CC ";
String [] separator string = longstring. Split (":");
For (INT I = 0; I <struct string. length; I ++ ){
System. Out. println (Bytes string [I]);
}

Obtained: AA, BB, and CC.

It can be proved that there is no problem, but if longstring = "AA | BB | cc", there is a problem. The Code is as follows:

String longstring = "AA | BB | cc ";
String [] separator string = longstring. Split ("| ");
For (INT I = 0; I <struct string. length; I ++ ){
System. Out. println (Bytes string [I]);
}

Then we get a, a, |, B, B, |, C, and C. The problem arises. I don't know why.

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.