Split Method In Java

Split Method In Java | The split() is a built-in method in the Java String class, which is used to divide the string. It usually uses a regular expression to divide the string into multiple substrings. It divides the string and returns the array of substrings. It breaks the substring according to the mentioned regular expression. First, it searches for the pattern given & if the pattern is matched then extracts the substring. In the String class, the substring() method was introduced in the Java1.4 version.

Prototype of substring() method in Java String class:- public String[] split(String regex)

  • Parameter:- regex, the delimiting regular expression
  • Return:- the array of strings computed by splitting this string around matches of the given regular expression
  • throws:- PatternSyntaxException if the regular expression’s syntax is invalid. It is an unchecked exception.

For example, let us consider the below string:-

String string = "Java Programming";

// now we want to split the string from “r” 
String[] substrings = string.split("r");

System.out.println(Arrays.toString(substrings));

Output:-

[Java P, og, amming]

Observe the output, the split() method has removed the character “r” from the string and returned an array of substrings. We have stored the returned array of substrings into the “substrings” variable and displayed them using the Arrays.toString() method. The toString() method of the Arrays class converts the array to string. And to use the Arrays class in the Java program, first, you have to import the Arrays class.

Java Split String By Space

To split the string by space just mention a space inside the split method i.e. pass "" in the split method. Since the return type of split() method in Java is an array of substrings after removing the characters given in the pattern therefore we will get the array of substrings. The program works as follows.

Java Program to Split String By Space

class Main {
   public static void main(String[] args) {
      String string = "Learn Java Programming Language";
      String[] substrings = string.split(" ");
      for (String str : substrings) {
         System.out.println(str);
      }
   }
}

Output:

Learn
Java
Programming
Language

Java Split String By Comma

Now, we will develop a Java program to split the string based on the comma. The split() method will produce the array of substrings after removing the comma “,” & splitting the string.

Java Split String By Comma

class Main {
   public static void main(String[] args) {
      String string = "Learn,Java Programming,KnowProgram";
      String[] substrings = string.split(",");
      for (String str : substrings) {
         System.out.println(str);
      }
   }
}

Output:

Learn
Java Programming
KnowProgram

Java Split String Into Array

We can split a String into an array based on the various patterns. In the below program the given string is split based on space.

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      String string = "Know Program website helps to learn " 
             + "various programming language effectively.";

      String[] strArray = string.split(" ");

      System.out.println(Arrays.toString(strArray));
   }
}

Output:-

[Know, Program, website, helps, to, learn, various, programming, language, effectively.]

We have stored the returned array of substrings into the “strArray” variable and displayed them using the Arrays.toString() method. The toString() method of the Arrays class converts the array to string.

Java Split String By Delimiter

By using a delimiter we can split the string containing delimiter characters. Delimiters are the character that splits strings separately into tokens. The whitespace character is a default delimiter in Java. In the below example we have used slash ‘/’ as a delimiter. 

Java Program to Split String By Delimiter

import java.util.*;

public class Main {
  public static void main(String[] args) {
    String string = 
     "know/program/website/programming/language/java/string/split";

    String[] strArray = string.split("/");

    System.out.println(Arrays.toString(strArray));
  }
}

Output:-

[know, program, website, programming, language, java, string, split]

Java Split String By Dot

In Java, the dot character (.) is used to represent all the characters in the string. If you split a string by passing "." in the split() method then it will return an empty array of strings. If we want to specify it as the escape characters then we have to use backslash with a dot ("\\.").

Java Program to Split String By Dot

import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    String string = "www.knowprogram.com";
    
    // split based on dot (.) character
    String[] strArray = string.split("\\.");
    System.out.println(Arrays.toString(strArray));
    
    // split each character
    String[] characters = string.split(".");
    System.out.println(Arrays.toString(characters));
  }
}

Output:

[www, knowprogram, com]
[ ]

Note that on calling split(“.”) the split method returns an empty array of strings because in regex the dot represents any character which means we want to split the string based on each character available in the string. Therefore at the end, we get an empty array of strings.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.