Java String split() with examples

The string split() method is used to break down a given string into substrings as determined by a given delimiter or a regular expression.

The returned array of substrings depicts the order in which the substrings are contained in the string. If the delimiter input/regular expression does not match any part of the input string, then an array with just one element, the input is returned.

There are 2 variations of this method as discussed below

1.    public String[] split(String regex,int limit)
2.    public String[] split(String regex)

1.    public String[] split(String regex,int limit)

  -The limit determines how many times the regex is applied therefore influencing the length of the results array.

    Limit values can be of the types as discussed below

   a)Limit greater than 0(limit>0) - In this case the limit is applied a maximum of limit-1 times.The result will contain all elements after the last         match (after the limit-1) iteration if more matches exist.

   b)Limit negative (limit<0) - The regex is applied as many times as possible.

  c)Limit is equal to 0(limit=0) - The regex is applied as many times as possible with trailing spaces discarded.

 2.   public String[] split(String regex)

This is equivalent to split(regex,0) therefore trailing spaces are not included in the resulting array.

This two variations throw PatternSyntaxException if an invalid regex is passed.

Examples with explanations

Example 1-Limit -0

public class StringSplit1 {

    public static void main(String[] args) {

        String str = "super@glue@is@sticky";

        System.out.println(Arrays.toString(str.split("@", 0)));

    }

}

The above code outputs

[super, glue, is, sticky]

The output above is same if we had    System.out.println(Arrays.toString(str.split("@")));

Explanation-understanding the output: To understand how the above output was arrived at think of the processing as the following series of iterations

Input = super@glue@is@sticky

On iteration 1 we have

[super, glue@is@sticky]-This iteration breaks at the first @, we have the first substring as super and the other substring as glue@is@sticky

On iteration 2 we have

[super,glue, is@sticky]-This iteration breaks at the second @,we now have substrings super ,glue and is@sticky

On iteration 3 we have

[super,glue,is,sticky]-This is the final iteration which gives as the result.

Example 2-Limit <0

public class StringSplit2 {

    public static void main(String[] args) {

        String str = "super@glue@is@sticky";

        System.out.println(Arrays.toString(str.split("@",-2)));

    }

}

The above code outputs

[super, glue, is, sticky]

Explanation-same as Example 1 above

Example 3-Limit >0 [limit =1]

Incase where limit =1,the resulting array contains 1 element which is just the input.This is because regex matching happens limit-1=1-1=0 times.

public class StringSplit3 {

    public static void main(String[] args) {

        String str = "super@glue@is@sticky";

        System.out.println(Arrays.toString(str.split("@",1)));

    }

}

The above program outputs

 [super@glue@is@sticky]

Example 3-Limit >0 [limit =2]

public class StringSplit4 {

    public static void main(String[] args) {

        String str = "super@glue@is@sticky";

        System.out.println(Arrays.toString(str.split("@",2)));

    }

}

The above program outputs

[super, glue@is@sticky]

Explanation:

Regex matching occurs 2-1=1 times hence only 1 split happens,the first split which returns the above result.

Example 4-Interesting scenario-negative limit with delimiter repeating ‘x’ times at end of input

public class StringSplit5 {

    public static void main(String[] args) {

        String str = "super@glue@is@sticky@@@";

        System.out.println(Arrays.toString(str.split("@",-1)));

    }

}

 

The above program outputs

[super, glue, is, sticky, , , ]

Explanation:

On negative limit,the regex matches as many times as possible.

On iteration 1,

We have [super, glue@is@sticky@@@]-super is left of first @ so it’s the first element of resultant array

On iteration 2,

We have [super,glue,is@sticky@@@]-glue is left of second @ so its second element of resultant array

On iteration 3,

We have [super,glue,is,sticky@@@]-is left of the third @ so it’s the third element of resultant array

On iteration 4,

[super,glue,is,sticky,@@]-sticky is left of fourth @ of array so it’s the fourth element of resultant array

On iteration 5,

[super,glue,is,sticky, ,@]-we have , ,(empty string “”) after sticky.On matching the first @ of @@ at index 4 of iteration 4,we are left with an empty string “” to the left.That is why we have , ,.

On iteration 6

[super,glue,is,sticky, , ,]-The final match happens on the final @ at index 5 of result of iteration 5.We are left with an empty string “” to the left which is added to the result hence our final result.

When the above program is run with limit=0,the output will be [super, glue, is, sticky] as the final two empty strings “” will be discarded.

Example 5- String split() with multiple delimiters

-When we want to split based on more than one delimiter,we can formulate a regex that incorporates the delimiters

e.g on the below program we want to split based on @ and #

public class StringSplit6 {

    public static void main(String[] args) {

        String str = "super@glue@is@sti#cky@@@";

        System.out.println(Arrays.toString(str.split("[@#]+",0)));

    }

}

The output is [super, glue, is, sti, cky]. The regex used above specifies we split at where we have one or more occurences of either @ OR  # delimiters.

Applications of split()

1.Can be used to separate  related configuration parameters  values in properties files and or in databases e.g. suppose we have a requirement to populate a request template for performing funds transfer in a core banking integration, we can have a property named bulkFundsTransfer that can hold related bulk transfer property values e.g companycode,settlement type and overdraw allowed i.e bulkFundsTransfer=CODE^IMMEDIATE^NO. The property can be read, and the independent values extracted by splitting the value by the ^ delimiter.

About the Author - John Kyalo Mbindyo(Bsc Computer Science) is a Senior Application Developer currently working at NCBA Bank Group,Nairobi- Kenya.He is passionate about making programming tutorials and sharing his knowledge with other software engineers across the globe. You can learn more about him and follow him on  Github.