The use of the split function in Java

Source: Internet
Author: User

The Split function in Java is used to split a string by a specified character (string) or regular, and the result is returned as an array of strings;
For example:
String str= "[email protected]";
String[] A = Str.split ("@");
SYSTEM.OUT.PRINTLN ("Processing result:" +a[0]+ "," +a[1]); The output is: processing result: 1234,ABC

For segmented characters (strings), usually common, ordinary, no problem;

But for some special characters, if the character (string) is exactly part of the regular, it needs to be escaped to be used.

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

For example:
Want to use | Vertical bar to split a character, because | itself is a part of the regular expression, so need \ to escape, because the escape using \, and this \ is exactly the character of the regular expression, so you have to use a \, so need two \ \.
String str= "5678| XYZ ";
String[] B = Str.split ("\\|"); Notice here with two \ \ instead of a \
SYSTEM.OUT.PRINTLN ("Processing result:" +b[0]+ "," +b[1]); The output is: processing result: 5678,xyz

Take a look again:
String str= "5678| XYZ ";
String[] B = Str.split ("|"); Note that the character is part of a regular expression, directly using |
String x= "processing result:";
for (int i=0;i<b.length;i++) {
X=x+b[i]+ ",";
}
SYSTEM.OUT.PRINTLN (x); The output is: processing result: 5,6,7,8,|,x,y,z,

Java Sample code: HTTP://WWW.EYESOURCECODE.COM/F/JAVASOURCECODE/1

The use of the split function in Java

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.