Java about split split string, empty string cannot get the problem
- 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 ("\\|");
- int row = 1;
- for (String s:num) {
- System.out.println (row+++ "=" +s);
- }
- }
- }
There are three of the most | | |, running 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
To view the API, there is a
- Public string[] Split (String regex, int limit);
The limit parameter controls the number of times the pattern is applied, thus affecting the length of the resulting array
if the limit n is greater than 0, then the pattern applies n>-1 times, the length of the array is not greater than N, and the last entry of the array will contain all inputs except the last match delimiter
if n non-positive, the number of times the pattern is applied is unlimited and the array can be any length
if n is zero, the number of times the pattern is applied is unlimited, and the array can be any length,and discards the trailing empty string
Modify the code to
- 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); Use 1 as a parameter here
- int row = 1;
- for (String s:num) {
- System.out.println (row+++ "=" +s);
- }
- }
- }
Run the result as
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=
Results are normal.
Java about split split string, empty string cannot get the problem