Question about separator and array length of "|" in string. Split () in Java

Source: Internet
Author: User

 

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 ,,,,

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.