Source http://java2000-net.iteye.com/blog/242162
First look at the source code Java code/** * * @author Zhaoqing <a href= "http://www.java2000.net" >www.java2000.net</a> * * * * clas s T {public static void main (string args[]) {string num[] = new STRING[11]; String sline = "101494|360103660318444|2008/06/17| Zhou Runying |1292.0|3085.76|2778.28|912.91|106.0| | |"; num = Sline.split ("\\|"); int row = 1; for (String s:num) {System.out.println (row+++ "=" +s); } } }
Run result is
1=101494
2=360103660318444
3=2008/06/17
4= Zhou Runying
5=1292.0
6=3085.76
7=2778.28
8=912.91
9=106.0
View API, there is a Java code public string[] Split (String regex, int limit);
The limit parameter controls the number of application modes, thus affecting the length of the resulting array.
if the limit n is greater than 0 , then the pattern applies at most n>-1 times, the length of the array is not greater than N, and the last entry of the array will contain all input except the last matching delimiter.
if n is not positive , the number of times the mode is applied is unrestricted, and the array can be of any length.
if n is zero , then the number of applied modes is unrestricted, the array can be any length, and the trailing empty string is discarded. Modify code to
Java Code/** * * @author Zhaoqing <a href= "http://www.java2000.net" >www.java2000.net</a> * * */class T { public static void Main (string args[]) {string num[] = new STRING[11]; String sline = "101494|360103660318444|2008/06/17| Zhou Runying |1292.0|3085.76|2778.28|912.91|106.0| | |"; num = Sline.split ("\\|",-1); Here use-1 as the parameter int row = 1; for (String s:num) {System.out.println (row+++ "=" +s); } } }
Run results
1=101494
2=360103660318444
3=2008/06/17
4= Zhou Runying
5=1292.0
6=3085.76
7=2778.28
8=912.91
9=106.0
10=
11=
12=
Java string delimiter split/stringtokenizer <!> comparison Two representations use the Split function: String s = new string ("2_8_7_4_3_9_1"); String [] arr = S. Split ("_"); Using the StringTokenizer class: String s = new string ("2_8_7_4_3_9_1"); StringTokenizer Commatoker = new StringTokenizer (S, "_"); String