Java split () function

Source: Internet
Author: User

Such as string: http://www.vvcc.abc.com: 8080/
When "." is used as a string array, the method is as follows:

String domainNam = "http://www.vvcc.abc.com: 8080 /";
String [] splitStr = domainNam. split ("\\.");

It must be escaped before it can be separated, simply using domainNam. split ("."); the length of the structure string array is 0;

 

 

To use String. split in Java but do not want to be disturbed by special characters of the regular expression, you can do this:

Java code
  1. // String str; String splitStr;
  2. String [] result = str. split (Pattern. quote (splitStr ));

 
For

Java code
  1. String str = "a | bcd | efg | higk ";
  2. String [] strs = str. split ("| ");

This seemingly correct code has a big problem. The root cause of the problem is the incorrect understanding of the split method of string objects in JAVA.

When writing javascript or other language programs, most of the parameters in split refer to separator strings or separator characters, while in JAVA, they are not. Their parameters are the separator rules, that is, a regular expression.

You can use:

Java code
  1. String [] strs = str. split ("\\| ");

Character '|' indicates a replacement in a regular expression. A separate '|' indicates that the program is split by each character, the final splitting result is to split each character in str separately, instead of separating them at '|.
But the most important thing is that the split parameter in JAVA is a regular expression!

Bytes ---------------------------------------------------------------------------------------
The java. lang package contains the String. split () method, and an array is returned.
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 ");

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.