How to split a String in Java with Delimiter

In Java, the split() method splits a string based on a given regular expression and returns a string array. The split method divides a string depending on a string delimiter defined as a regular expression. This method returns a string array after splitting against the specified regular expression.



Using String.split method

The string split() method allows to break the string based on given regular expression. The split method returns a string array contains the splitter string as items in the array.

package com.yawintutor;

import java.util.Arrays;


public class SplitExample {
	public static void main(String[] args) {
		String value="123-456-789-01234";
		String[] array= value.split("-");
		System.out.println(Arrays.toString(array));
	}
}

Output

[123, 456, 789, 01234]


Using String split() limited split

The default string split method divides a string into multiple strings using a regular expression as the delimiter. The limit option is used to set a limit on the number of splits that can be created. The following example will demonstrate how to limit the number of splits. The last split in the array will contains remaining string that will have the delimiter characters.

package com.yawintutor;

import java.util.Arrays;


public class SplitExample {
	public static void main(String[] args) {
		String value="123-456-789-01234";
		String[] array= value.split("-",3);
		System.out.println(Arrays.toString(array));
	}
}

Output

[123, 456, 789-01234]


Using String split() without trailing empty String

When using the String split method with the limit option set to 0, the string will be split into multiple strings and the empty strings at the end of the array will be removed. Only empty strings will be cut with the 0 limit option. This option does not remove the empty string at the beginning and the empty string in between the string.

package com.yawintutor;

import java.util.Arrays;

public class SplitExample {
	public static void main(String[] args) {
		String value="123,456,789,01234,,,";
		String[] array= value.split(",",0);
		System.out.println(Arrays.toString(array));
	}
}

Output

[123, 456, 789, 01234]


Split character to end up in left hand side

If you want the split character to be appended at the end of the splitter string, positive look behind option is used in the regular expression by prefixing the group(?<=). The split delimiter character will be appended to the end of each string in the string array except the last one.

package com.yawintutor;

import java.util.Arrays;


public class SplitExample {
	public static void main(String[] args) {
		String value="123-456-789-01234";
		String[] array= value.split("(?<=-)");
		System.out.println(Arrays.toString(array));
	}
}

Output

[123-, 456-, 789-, 01234]


Split character to end up in right hand side

If you want the split character to be appended at the start of the splitter string, positive look ahead option is used in the regular expression by prefixing the group(?=). The split delimiter character will be prefixed to each string in the string array except the first one.

package com.yawintutor;

import java.util.Arrays;

public class SplitExample {
	public static void main(String[] args) {
		String value="123-456-789-01234";
		String[] array= value.split("(?=-)");
		System.out.println(Arrays.toString(array));
	}
}

Output

[123, -456, -789, -01234]


Split by Regex Special characters

In the String split, a regular expression is used as a string delimiter. If you want to split by a delimiter that is a special character in the regular expression, the String will not be broken based on the delimiter. In this case, the escape sequence should be used to match the regular expression’s special character. In the example below, the dot character (.) is used to split the string. Because it matches any character in a regular expression, the dot character will be escaped using the “” in the split method.

package com.yawintutor;

import java.util.Arrays;

public class SplitExample {
	public static void main(String[] args) {
		String value="123.456.789.01234";
		String[] array= value.split("\\.");
		System.out.println(Arrays.toString(array));
	}
}

Output

[123, 456, 789, 01234]