Use of the split function in Java, the pluplit Function

Source: Internet
Author: User

Use of the split function in Java, the pluplit Function

The split function in Java is used to split a string by a specified character (string) or regular expression, and the result is returned in the form of a string array;
For example:
String str = "1234 @ abc ";
String [] a = str. split ("@");
System. out. println ("processing result:" + a [0] + "," + a [1]); // The output result is: 1234, abc.

For separated characters (strings), it is usually common and common, and there is no problem;

However, for some special characters, if the character (string) is exactly a part of the regular expression, it must be escaped before it can be used,

These characters include |, +, *, ^, $,/, |, [,], (,),-,., \, because they are part of a regular expression, if you want to use the character itself, these characters must be escaped to represent itself;

For example:
If you want to use | vertical bars to separate a character, because | itself is a part of a regular expression, you need to use \ to escape the character, and this \ is exactly the character of the regular expression, therefore, we need to use one \, so we need two \\.
String str = "5678 | XYZ ";
String [] B = str. split ("\ |"); // note that two \ instead of one \
System. out. println ("processing result:" + B [0] + "," + B [1]); // The output result is: 5678, XYZ

Let's take a look:
String str = "5678 | XYZ ";
String [] B = str. split ("|"); // use it directly. | this character is part of a regular expression,
String x = "processing result :";
For (int I = 0; I <B. length; I ++ ){
X = x + B [I] + ",";
}
System. out. println (x); // The output result is: 5, 6, 7, 8, |, X, Y, Z,

Java sample code: http://www.eyesourcecode.com/f/JavaSourceCode/1

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.