The string's split () method introduces

Source: Internet
Author: User
Tags expression include regular expression split

Actually did not intend to write such a blog, but yesterday in the forum, found a post, and then I put the content of the post in the group made a pass, the results have been surprising results, so here is simply to share the split () method, so that the people will not have this basic knowledge error!

Next, I saw the post content 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);

Everyone can also first talk about their heart first reaction to the answer is what, record down, and then slowly to look down, and finally compared to the results! This kind of problem, in a lot of interview process, interview technology person will ask this very representative basic knowledge!

If you often see the JDK API, this problem should not make mistakes!

Public string[] Split (string regex) splits this string according to the match of a given regular expression.   
the method acts as if the two-parameter split method is invoked using the given expression and the constraint parameter 0来. Therefore, the resulting array does not include the trailing empty string.   
      
For example, the string "Boo:and:foo" uses these expressions to produce the following results:   
      
Regex result   
: {"Boo", "and", "foo"}   
o {"B", "", ": And:f"}   
      
      
parameter:  
regex-bounding regular expression   
returns:  
an array of strings, which is the root Split this string determined by matching a given regular expression   
:   
patternsyntaxexception-If the syntax of the regular expression is not valid

This has been introduced in the very clear, do not know whether the careful person found this sentence is very important!

The final 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? Here is not much to say, the answer is in the "derived array does not include the end of the empty string," from a slowly understand!

URL Address: http://www.bianceng.cn/Programming/project/201602/49623.htm

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.