Java split method
1. interval no. "."
(1) str. split (".")
String str = "10.156.35.87"; String [] str1 = str. split ("."); for (int I = 0; I
The running result is blank, that is, no result is output.
(2) str. split ("\\.")
String str = "10.156.35.87"; String [] str1 = str. split ("\."); for (int I = 0; I
The running result is as follows:
0th: 10 1st: 156 2nd: 35 3rd: 87
2. Single vertical number "|"
(1) str. split ("| ")
String str = "10 | 156 | 35 | 87"; String [] str1 = str. split ("|"); for (int I = 0; I
The running result is as follows:
0th: 1st: 1 2nd: 0 3rd: | 4th: 1 5th: 5 6th: 6 7th: | 8th: 3 9th: 10th: | 11th: 8 12th: 7
(2) str. split ("\ | ")
String str = "10 | 156 | 35 | 87"; String [] str1 = str. split ("\\|"); for (int I = 0; I
The running result is as follows:
0th: 10 1st: 156 2nd: 35 3rd: 87
3. asterisk (*)
(1) str. split ("*")
String str = "10*156*35*87"; String [] str1 = str. split ("*"); for (int I = 0; I
The running result is as follows:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0*^at java.util.regex.Pattern.error(Pattern.java:1924)at java.util.regex.Pattern.sequence(Pattern.java:2090)at java.util.regex.Pattern.expr(Pattern.java:1964)at java.util.regex.Pattern.compile(Pattern.java:1665)at java.util.regex.Pattern.
(Pattern.java:1337)at java.util.regex.Pattern.compile(Pattern.java:1022)at java.lang.String.split(String.java:2361)at java.lang.String.split(String.java:2403)at com.you.model.JavaSplit.main(JavaSplit.java:29)
(2) str. split ("\\*")
String str = "10*156*35*87"; String [] str1 = str. split ("\ *"); for (int I = 0; I
The running result is as follows:
0th: 10 1st: 156 2nd: 35 3rd: 87
4. plus sign "+"
(1) str. split ("+ ")
String str = "10 + 156 + 35 + 87"; String [] str1 = str. split ("+"); for (int I = 0; I
The running result is as follows:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0+^at java.util.regex.Pattern.error(Pattern.java:1924)at java.util.regex.Pattern.sequence(Pattern.java:2090)at java.util.regex.Pattern.expr(Pattern.java:1964)at java.util.regex.Pattern.compile(Pattern.java:1665)at java.util.regex.Pattern.
(Pattern.java:1337)at java.util.regex.Pattern.compile(Pattern.java:1022)at java.lang.String.split(String.java:2361)at java.lang.String.split(String.java:2403)at com.you.model.JavaSplit.main(JavaSplit.java:29)
(2) str. split ("\ + ")
String str = "10 + 156 + 35 + 87"; String [] str1 = str. split ("\ +"); for (int I = 0; I
The running result is as follows:
0th: 10 1st: 156 2nd: 35 3rd: 87