The split () method of String explores and reveals the big secret

Source: Internet
Author: User

In fact, did not intend to write such a blog post, but yesterday in the forum, found a post, and then I put the content of the post in the group sent a pass, the results appeared to surprise the results, so here is simple to share the split () method, lest everyone will appear such a basic knowledge error!

Next, the content of the post you saw yesterday:

string[] str1 = ";;;". Split (";"); string[] str2 = "; A;;". Split (";"); string[] Str3 = ";; A; ". Split (";"); System.out.println (str1.length); System.out.println (str2.length); System.out.println (str3.length);

We can also first talk about their hearts first reaction to the answer is what, recorded, and then slowly have to look down, and finally compare the results! Such a problem. In a lot of interview process, interview technology people will ask such a very representative of the basic knowledge.


If you look at the JDK API so often, this problem should not be made wrong!

Public string[] Split (string regex) splits this string based on a match of the given regular table. The method acts as if the two-parameter split method is called with the given expression and the limit parameter.

Therefore, the resulting array does not contain a trailing empty string.

For example, the string "Boo:and:foo" uses these expressions to produce the following result: The regex result: {"Boo", "and", "foo"} o {"B", "", ": And:f"}: Regex-bound normal table return: number of strings Group. It is the throw that is determined by the matching split of this string based on the given regular table: patternsyntaxexception-Assuming that the syntax for the normal form is invalid


This has been introduced very clearly. I do not know if there is any careful person to find this very important words.

Finally, the result is:

The length of the STR1 is: 0

The length of the STR2 is: 2

The length of the STR3 is: 3

Is your answer right? There's not much to say here. The answer is in "the resulting array does not contain the trailing empty string ", from a slowly understanding!

The split () method of String explores and reveals the big secret

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.