1, "|" as the separator public static void main (string [] ARGs) {string STR = "abcdef | ghijk | lmno | pqrst |"; string [] array = Str. split ("|"); // use "|" as the separator system. out. println ("size =" + array. length); For (string ST: array) {system. out. print (ST + ",") ;}} after the preceding function is run, the result is: size = 25, A, B, C, D, E, F, |, G, H, i, J, K, |, L, M, N, O, |, P, Q, R, S, T, |, this is not what we want. in this case, set string []
Array = Str. split ("|"); changed to string [] array = Str. split ("\ |"); to get the expected result. the following are Code : Public static void main (string [] ARGs) {string STR = "abcdef | ghijk | lmno | pqrst |"; string [] array = Str. split ("\ |"); system. out. println ("size =" + array. length); For (string
St: array) {system. out. print (ST + ",") ;}} the running result is as follows: size = 4 abcdef, ghijk, lmno, pqrst, 2, and the array length, let's look at the example. Public static void main (string [] ARGs) {string STR = "abcdef, ghijk, lmno, pqrst,"; string [] array = Str. split (","); system. out. println ("size =" + array. length );
For (string ST: array) {system. out. print (ST + ",") ;}} running result: size = 4 abcdef, ghijk, lmno, pqrst, the null values following the string, if needed, change to public static void main (string [] ARGs) {string STR = "abcdef, ghijk, lmno, pqrst,"; string [] array = Str. split (",",-1); system. out. println ("size =" + array. length );
For (string ST: array) {system. Out. Print (ST + ",") ;}} running result: size = 7 abcdef, ghijk, lmno, pqrst ,,,,