Java Split String with Split() Method [Examples]

The string split() method in Java splits a given string around matches of the given regular expression. To split a string in java into multiple Strings given the delimiter that separates them use the java method split(regex). The returned object is an array that contains the split Strings.

We can also pass a limit parameter split(regex,limit) to the number of elements in the returned array. If we pass 0 as a limit, then the method will behave as if we didn’t pass any limit and returning an array containing all elements that can be split using the passed delimiter.

Following are the two variants of split() method in Java:

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

1. Split String with any delimiter


The split() method variant without limit parameter takes a regular expression as a parameter and breaks the given string around matches of this regular expression regex. Here by default limit is 0.

package com.kodehelp.java;

/**
 * Created by https://kodehelp.com
 * Date: 06/22/2019
 */
public class SplitStringInJava {

	public static void main(String[] args) {
		
		String phone = "132-476-9483"; // replace this with your string
		String[] output = phone.split("-");//replace this with your delimiter
		for(String s: output)
		System.out.println(s);
		
	}

}
OUTPUT
132
476
9483

2. Split with any delimiter and limit


The split() method variant with the limit parameter takes a regular expression and the limit as a parameter. The limit parameter will control the number of times the pattern is applied and therefore affects the length of the resulting array.

Limit parameter can have 3 values:

  • Limit > 0: Here, the pattern will be applied at most limit-1 times, the resulting array’s length will not be more than n, and the resulting array’s last entry will contain all input beyond the last matched pattern.
  • Limit < 0: In this case, the pattern will be applied as many times as possible, and the resulting array can be of any size.
  • Limit = 0: Here, the pattern will be applied as many times as possible, the resulting array can be of any size, and trailing empty strings will be discarded.
package com.kodehelp.java;

/**
 * Created by https://kodehelp.com
 * Date: 06/22/2019
 */
public class SplitStringInJava {

	public static void main(String[] args) {
		
		String phone = "132-476-9483"; // replace this with your string
		String[] output = phone.split("-",2);//replace this with your delimiter
		for(String s: output)
		System.out.println(s);
		
	}

}
OUTPUT
132
476-9483

3. String split with period or dot as delimiter


Regular Expression has 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called “metacharacters“.

Note: To split a String with the period or dot and this character is a special character in the regex, you have to escape it either with a double backslash \\. or uses the Pattern.quote method.

package com.kodehelp.java;

/**
 * Created by https://kodehelp.com
 * Date: 06/22/2019
 */
public class SplitStringInJava {

	public static void main(String[] args) {
		
		String phone = "132.476.9483"; // replace this with your string
		String[] output = phone.split("\\.");//replace this with your delimiter
		for(String s: output)
		System.out.println(s);
		
	}

}
OUTPUT
132
476
9483
References: