Example of the split method using the limit parameter

To understand the impact of the limit parameter, let's take a comma-separated input string with two trailing commas:

fox,tiger,wolf,, 

We can call the split method in two ways. We can call the split method with limit=0:

String[] arr = input.split(",", 0); 

We can also call the single parameter split method call as:

String[] arr = input.split(","); 

It splits the input string around a comma and the trailing empty strings are discarded, with the following values being returned by the split method:

"fox" "tiger" "wolf" 

Now, let's call the split method with limit=1:

String[] arr = input.split(",", 1); 

It splits the input string around a comma and then gets a single element in the resulting ...

Get Java 9 Regular Expressions now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.