Java String Split Method

The Java String Split Method 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 String Split method returns those substrings in a String array.

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

Java String Split Method Syntax

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

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

public String[] split(String regex); // It will return String 

//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 used to restrict the output.

public String[] split(String regex, int limit); // It will return String 

//In order to use in program
String_Object.split(String regex, int limit);
  • String_Object: Please specify the valid string on which we want to perform Splitting.
  • 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.

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

Java String Split Method Example

This Java program uses the Java String Split method to split the user-specified string object into String Array.

// Java String.split Example 
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

The following statement will call the public static String[] split(String regex) method 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.

String[] str1 = str.split(" ");

Next, We used substring “abc” as a separator for the Java Split function.

String[] str2 = str.split("abc");

The following Java statement is to print the String array elements to the output

arrayPrint(str2);

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

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

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 separator to split the string.

// Java String.split Example 
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("Splitting 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 Elelment = " + str);
		}
	}
}
Array Elelment = We
Array Elelment = are
Array Elelment = abc
Array Elelment = working in abc company since abc years
Splitting String using Word abc
Array Elelment = We are 
Array Elelment =  working in 
Array Elelment =  company since 
Array Elelment =  years

The following statement will call the public static String[] split(String regex, int limit) method to split the original string into individual characters. Here, we used the separator as empty space, and we are limiting the number of splits to 4.

String[] str1 = str.split(" ", 4);

Next, We used substring “abc” as a separator for the Java Split function, and we are limiting the number of splits to 4.

String[] str2 = str.split("abc", 4);

Comments are closed.