Java String split split special character __J2EE

Source: Internet
Author: User
Java String split split special character
You need to be aware of the following special characters (all require escape \ \)
"|" "." "*" "_" "+" ""
Api:
Public string[] Split (String regex) splits this string around matches of the given regular.
A parameter regex is a regular-expression match pattern rather than a simple string, and he may have unexpected results for some special characters,
For example, test the following code: Vertical Bar | Delimited string, you will not get the desired result string[] AA = "AAA|BBB|CCC". Split ("|");
The "|" divider string, while being able to execute, is not intended, "\\|" The correct result can be obtained after escaping.
string[] aa = "AAA|BBB|CCC". Split ("\\|"); (right)
Running with the vertical * separator string will throw the java.util.regex.PatternSyntaxException exception, with the plus sign +.
string[] aa = "AAA*BBB*CCC". Split ("*");
string[] aa = "AAA|BBB|CCC". Split ("\\*");(right)
Obviously, + * is not a valid pattern-matching rule expression, with the "\\*" "\\+" escaped to get the correct result.

Also, if you want to use the character in a string, you also need to escape. The first expression of "aaaa\bbbb" This string should be "AAAA\\BBBB",
If you want to separate, you should be able to get the correct result: string[] aa = "AAA\\BBB\\BCCC". Split ("\\\");(right)

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.