Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - How split(regex, limit) method actually works? [duplicate]

Tags:

java

string

split

I am trying to understand how the split method works and have a slight confusion about it. In this Example given in the documentation pages of oracle,

String str = "boo:and:foo";

String[] str1 = str.split("o",2);

Output
 b
 o:and:foo

This is easy to understand, that the string has been literally divided at the occurence of the first 'o'

but for

String[] str1 = str.split("o",3);

Output:
b

:and:foo 

How is this coming out as such?

like image 601
pluto20010 Avatar asked Nov 29 '17 12:11

pluto20010


People also ask

How does split regex work?

Split method, except that Regex. Split splits the string at a delimiter determined by a regular expression instead of a set of characters. The input string is split as many times as possible. If pattern is not found in the input string, the return value contains one element whose value is the original input string.

How does the split method works in Java?

The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the split Strings. We can also pass a limit to the number of elements in the returned array.

What does split () return if the string has no match Java?

split (separator, limit) , if the separator is not in the string, it returns a one-element array with the original string in it.

What is limit in Split Java?

The limit parameter is used to decide how many times we want to split the string. The limit parameter can take one of three forms, i.e it can either be greater than, less than or above zero.


2 Answers

What i understand from the documentation :

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. 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. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

This mean devise or cut it to n time on string s, so Lets analyse one by one to understand better :

Limit 1

String[] spl1 = str.split("o", 1);

This mean split it or cut it on just one string on the string o in this case you will get all your input :

[boo:and:foo]
 1

Limit 2

String[] spl1 = str.split("o", 2);

Which mean cut it one time on o so i will put a break in the first o

    boo:and:foo
-----^

in this case you will get two results :

[b,o:and:foo]
 1 2

Limit 3

String[] spl1 = str.split("o", 3);

Which mean cut it two times on the first o and on the second o

    boo:and:foo
1----^^--------------2

in this case you will get three results :

[b, ,:and:foo]
 1 2  3

Limit 4

String[] spl1 = str.split("o", 4);

Which mean cut it three times on the first, second and third o

     boo:and:foo
1_____^^      ^
       |___2  |___3

in this case you will get four results :

[b, ,:and:f,o]
 1 2 3      4

Limit 5

String[] spl1 = str.split("o", 5);

Which mean cut it four times on first, second, third and forth o

     boo:and:foo
1_____^^      ^^
       |___2  ||___4
              |____3

in this case you will get five results :

[b, ,:and:f, , ]
 1 2  3     4 5

Just a simple animation to understand more :

How split() method actually works?

like image 182
YCF_L Avatar answered Sep 20 '22 17:09

YCF_L


The second parameter represents the number of times the pattern is need to apply.

From Java Docs:

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. 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. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Example:

1) if the limit is set to zero (str.split("o",0)) then according to java docs the pattern will be applied as many times as possible so the result will be :

[b, , :and:f]

2) but if you set the limit to non zero value (e.g. 1 or 2) then the pattern will be applied n-1 times (e.g. for limit 1 pattern will be applied 0 time and for 2 it will apply 1 time) so the result will be below:

[boo:and:foo] // for str.split("o",1) applied 0 time.

[b, o:and:foo] // for str.split("o",2) applied 1 time.

[b, , :and:foo] // for str.split("o",3) applied 2 time.

like image 41
Raju Sharma Avatar answered Sep 20 '22 17:09

Raju Sharma