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
- // String str; String splitStr;
- String [] result = str. split (Pattern. quote (splitStr ));
For
Java code
- String str = "a | bcd | efg | higk ";
- 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
- 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 ");