Java String split Function

The Java split function is one of the String Methods, which is to split the original string into an array of substrings based on the separator that we specified. And the Function returns those substrings in a String array.

In this article, we will show how to write the String split function with an example. The syntax of the String split in Java Programming language is as shown below.

Java String split Function Syntax

This Java function splits the original string into an array of substrings based on the separator. And it returns them into a new String array. This string function will accept the regular expressions as the parameter to split the original string.

This Java split function accepts one argument of string type, and the argument (regular expression) is the separator that you want to use.

public String[] split(String regex); 

//In order to use in program
String_Object.split(String regex);

Java String split function will accept two arguments. The first argument (regular expression) is the separator you want to use, and the second integer value is useful to restrict the output.

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

//In order to use in program
String_Object.split(String regex, int limit);
  • Separator: Please specify the separator (such as empty space, ‘,’ or ‘.’) you want to use it. It can be string literal or Java Regular Expression.
  • Limit: Please specify the integer number. This argument will restrict the number of elements written by the array.

This method will not alter the original string. If we omit the second index, the String Method will start from the beginning and continue till the end.

Java String Split Function Example

It uses the Java Function to split the user-specified string object into Array. The following str1 statement will call the method with a single argument to split the original string into a group of words and store them in a string array. Here we used the separator as empty space.

Next, We used substring “abc” as a separator for the split function str2. The following Java statement is to print the String array elements to the output.

When the compiler reaches the arrayPrint(str2) statement, the compiler will jump to the following function. From the below code snippet, observe that we used the Foreach Loop to iterate the string Array. Then we are printing every array element using the System.out.println statement.

package StringFunctions;

public class SplitMethod {
	public static void main(String[] args) {
		String str = "We are abc working in abc company";
		
		String[] str1 = str.split(" ");
		arrayPrint(str1);
		System.out.println("Splitting String using Word abc");
		String[] str2 = str.split("abc");
		arrayPrint(str2);

	}
	public static void arrayPrint(String[] anStringArray) {
		for (String str: anStringArray) {
			System.out.println("Array Elelment = " + str);
		}
	}
}
Java String Split Method 1

Java String Split Method Example 2

This program is another example of the string split method. Here, we used the empty spaces and abc as the separators.

The following str1 statement will call the method with two arguments to separate the original string into individual characters. Here, we used the separator as empty space, and we are limiting the number of splits to 4.

Next, We used substring “abc” as a separator, and we are limiting the number of substrings to 4.

package StringFunctions;

public class SplitMethod2 {
	public static void main(String[] args) {
		String str = "We are abc working in abc company since abc years";
		
		String[] str1 = str.split(" ", 4);
		arrayPrint(str1);
		
		System.out.println("Separate String using Word abc");
		String[] str2 = str.split("abc", 4);
		arrayPrint(str2);
	}
	public static void arrayPrint(String[] anStringArray) {
		for (String str: anStringArray) {
			System.out.println("Array Element = " + str);
		}
	}
}
Array Element = We
Array Element = are
Array Element = abc
Array Element = working in abc company since abc years
Separate String using Word abc
Array Elelment = We are 
Array Elelment =  working in 
Array Elelment =  company since 
Array Elelment =  years

Comments are closed.