Java String Split

In java you might face a situation where you need to split String, based on some criteria. To fulfill this requirement Java has already provided two in-built methods to achieve this.

  1. Method 1 : split(String regex, int limit)
  2. Method 2 : split(String regex) (Internally calls above method with limit as 0)
    1. split(String regex, 0)

Parameter

regex (Regular Expression) : An expression(pattern) around which you would like to split given string.

limit : It will decide how many times given pattern will be applied while splitting the string.

  1. n>0 : pattern will be applied at most n-1 times.
  2. n=<0 : Pattern will be applied as many times as possible

Return Value

String[] : Array of Strings resulted after splitting string with above regular expression. Length of this array is depended on limit parameter.

Example

Output

Limit Option Example

Output

 

Leave A Comment

Your email address will not be published. Required fields are marked *