Thursday, 16 February 2017

Do You Really Understand java.lang.String.split(regex) Method ?

You are in surprise that why this post is here under "secretsinjava.blogspot.com"? What is the secret here? But believe me, the secret I am going to explore here is hardly known by someone.

We all know that String.split("regex") gives an array of string separated by the supplied "regex".  Now answer for the below statement:
"Alpha:Beta:Gama".split(":"); 
It is very simple and the value returned by split method is {"Alpha", "Beta", "Gama"}.

Now what about "Alpha:".split(":"), "Beta::".split(":")  and  "Alpha::Gama".split(":") ?
If you are thinking the answers will be like {"Alpha", ""} , {"Beta", "", ""} and {"Alpha":"":"Gama"} then you are absolutely wrong and you should know the secret of this.

Before going to the secret, I will tell the answers so that you will decide whether it is worth knowing or not. The answers will be like {"Alpha"},  {"Beta"} and {"Alpha","","Gama"}.

When we call the String.split("regex") method, this internally calls the below overloaded method with extra argument "limit" set to 0.

Here is the source code for reference :


Here the secret is all about he limit parameter. As per the Java API specification, 
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. i.e
when limit > 0 : the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter
when limit == 0 :  the pattern will be applied as many times as possible and the array can have any length, and trailing empty strings will be discarded
when limit < 0 : then the pattern will be applied as many times as possible, the array can have any length

Now let's see some examples:
 Output :

I hope it is clear about the secret in String.split("regex") method. Please feel free to ask/provide your doubt/comments. 

 <script src="https://github.paypal.com/gist/prajena/3f633b24cbc9699830344e09c51a6e6a.js"></script>