) Java concerning split string, null strings cannot be obtained

Source: Internet
Author: User
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 |, and the running 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

Check the API. There is a Public String [] Split (string RegEx, int limit );

The limit parameter controls the number of application modes, thus affecting the length of the result array.

If the limit N is greater than zero, The pattern applies n>-1 times at most, 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 positiveThe number of application modes is unlimited, and the array can be of any length.

If n is zeroThe number of times in the application mode is unlimited. The array can be of any length and the trailing null string will be discarded.

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); // here-1 is used as the parameter
Int ROW = 1;
For (string S: num ){
System. Out. println (row ++ "=" + S );
}
}
}

The running 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
10 =
11 =
12 =

The result is normal.

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.