How split() method works in java?

Viewed 574

My question is why the following program:

// Java program to demonstrate working of split(regex,
// limit) with high limit.
public class GFG
{
    public static void main(String args[])
    {
        String str = "geekss@for@geekss";
        String [] arrOfStr = str.split("s", 5);
    }
}

splits the string "geekss@for@geekss" into 5 substrings:{"geek", "", "@for@geek", "", ""}. According to me there should be 4 substrings:{"geek", "","@for@geek", ""}. Can someone clarify my doubt?

4 Answers

If you look carefully at the docs:

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.

So your resulting array contains two things:

  • a substring of your string which is followed by s (italic part)
  • whatever is left at the end of your string (bold part)

The reason you got {"geek", "", "@for@geek", ""} for the first four elements is because they are followed by s. The last "" you got is what is left after matching every s.

Note that the limit argument of 5 you passed is also related. As per the docs:

If the limit n is greater than zero then 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.

So the last matched delimiter is the s at the very end. After that there is still an empty string that it haven't checked.

Here is a pictorial explanation of what is happening:

^geek s  s @for@geek s  s  $
 geek |""| @for@geek |""|""

The position in between the final delimiter and $ is counted as an empty string match. Note that if your string started with s you would also pick up an empty string.

The trivial reason why we happen to see 5 matches is because your split limit is greater than or equal to 5, but that alone does not explain the output.

Split("s", 5) finds the 4 s chars and returns the 5 subtrings which are whatever exists between two sequentially 's' thus the empty substrings , or from start of string till first 's' and from last 's' till the end of the string. So you have 5 substrings.

If you take a look at @ tobias_k very usefull comment you have 4 's' chars and not 5, the second parameter (number 5) in split method tells how many substring are if we remove s, for 4 s there are 5 substring, if you try with any number greater than 5 you get same results since there are no other substrings to split.

Your string is "geekss@for@geekss", so if you consider 's' as your delimiter, you obtain:

"geek" + ['s'] + "" + ['s'] + "@for@geek" + ['s'] "" + ['s'] + ""

So you have ["geek", "", "@for@geek", "", ""]

Maybe the part that is confusing you is the last empty string. Think that the delimiter is always "contained" in the string. So if it occurs at its end, it is actually considered as "your_string" + your_delimiter + "".

The same would happen if your string began with "s": in that case, the first element of your split array would be "".

Related