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