String. Split ()

Source: Internet
Author: User
I have used some of them in the application. I will summarize them for your reference only:
1. If ". ", must be written as follows: string. split ("\\. "), in this way, the correct separation is not allowed using string. split (". ");
2. If "|" is used as the separator, it must be written as follows: string. split ("\ |"), in order to correctly separate, cannot use string. split ("| ");
"." And "|" are escape characters and must be added "\\";
3. If a string contains multiple delimiters, you can use "|" as a hyphen, for example, "acount =? And UU =? Or n = ?", Use string. Split ("and | or") to separate all three ");
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------

When the string. split method is used to separate strings, if some special characters are used as separators, the expected results may not be obtained.

Public String [] Split (string RegEx)
Splits this string around matches of the given regular expression.

The RegEx parameter is a regular-expression matching mode instead of a simple string. It may produce unexpected results for some special characters, such as the following testCode:

Use vertical bars | to separate strings and you will not get the expected results

String [] AA = "AAA | BBB | CCC". Split ("| ");
// String [] AA = "AAA | BBB | CCC". Split ("\\|"); in this way, the correct result can be obtained.

For (INT I = 0; I <AA. length; I ++ ){
System. Out. println ("--" + AA [I]);
}

Java. util. RegEx. patternsyntaxexception will be thrown. In other similar cases:

1) plus sign +
2) asterisk *
string [] AA =" AAA * BBB * CCC ". split ("\ *"); to get the correct result
3) and escape if you want to use the "\" character in the string. the expression "AAAA \ BBBB" should be "AAAA \ BBBB" first. If it is to be separated, the correct result should be obtained:
string [] AA = "AAA \ BBB \ BCCC ". split (\\\\);
4) full stop.

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.