×

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the split () technique for the String class. In this topic, we will learn how to split the String using the delimiter in Java. Similarly, we use the String Tokenizer and Scanner classes to split the String.

For splitting the String in Java with a delimiter. Firstly, we have to know what delimiter is.

In Java, the delimiter is the character that divides or splits the given String into tokens. It provides us to understand any character defined in Java as the delimiter. In Java, there are many splitting methods are there. One of the methods is the white space character, and this character is considered the default delimiter.

Before going to the programming code, let's see the theory of the given concept.

String: As we know, String is the Set of characters stored in the quotes. I.e., “java”. In Java, the String is provided with two types.

They are tokens and delimiters. Whereas the tokens are knowns as the words which make meaningful sense, the delimiters are known as the characters that divide or split into the tokens.

Let's see this topic in more detail with the examples:

To deal with the delimiters in Java, we must know the basics of Java regular expressions. It is significant whenever the delimiter is performed as the special characters like [.], [,], [|].

Example: Let's see the example for delimiter

Consider a string as Google is a search engine.

From the above String, the tokens we consider are Google, is, search, engine, and the delimiter we performed here are the white spaces between the two tokens.

How to Split a String in Java with Delimiter?

For splitting the String in Java, it provides the three methods:

  • Using Scanner.next() Method
  • Using String.split() Method
  • Using StringTokenizer Class
  • Firstly, let's see about the Scanner.next () Method:

Scanner.next() method is one of the scanner classes. This method finds the token and returns it to the next token from Scanner. It is used to separate or split the String into the tokens using a delimiter. The total token is considered by the info that matches the delimiter design.

Now let's see a simple example:

import java.util.Scanner;
public class Demo3
{
public static void main(String args[])
{
Scanner scan = new Scanner("Google/Is/The/Search/Engine");
scan.useDelimiter("/");
while(scan.hasNext())
{
System.out.println(scan.next());
}
scan.close();
}
}

Output:

How to Split the String in Java with Delimiter

The String has been divided into tokens with delimiters in the above program.

1. The second method is Using String.split() Method:

The split () method for the String class is used to part a string into a variety of String objects given the particular delimiter that matches the ordinary expression.

String str= "java,is,platform,dependent,programming,language"; 

The commas split the above String. For splitting, we use the following expression in our code.

Syntax: 

String [] tokens=sc.split(",");

For the above syntax, the String has been divided into tokens with special delimiter characters like comma (,).

Syntax:

public String[] split(String regex);

This method is used to pass the delimiter for the regular java expressions. For this program

It raises the exception. If the invalid syntax has presented in the program, it throws the exception called pattern syntax expression.

Let's see a simple code for the following syntax:

import java.util.Scanner.*;
public class Demo3
{
public static void main (String args[])
{
String s =   "java,is,platform,dependent,programming,language";
String[] stringarray = s.split(",");
for(int i=0; i< stringarray.length; i++)
{
System.out.println(stringarray[i]);
}
}
}

Output:

How to Split the String in Java with Delimiter

2. Now let's see with the third method using StringTokienizer Class:

As we know, that java StringTokienizer is the Superclass which is defined in the util.Scanner package. Mostly we use the split () method rather than the StringTokienizer class in the programming language. 

Now let's see the simple code for StringTokienizer class:

import java.util.StringTokenizer;
public class Demo3
{
public static void main(String[] args)
{
String str = "java/is/object/oriented/programming/language";
StringTokenizer tokens = new StringTokenizer(str, "/");
while (tokens.hasMoreTokens())
{
System.out.println(tokens.nextToken());
}
}
}

Output:

How to Split the String in Java with Delimiter

These three methods are to split the String in Java using the delimiters.


Related Topics

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

How to set timer in Java

In this article, you will be very well equipped with the knowledge to set timer in java. The timer in java can be set by using timer class provided by...

3 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Check if the given array is mirror inverse in Java

The primary objective is to check whether the given array is mirror inverse or not. The values and position of the specified array are switched, and a duplicate array is created....

3 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result. Syntax public static int...

2 minutes read.

Advanced Java Viva Questions

One of the more difficult languages available now is Java. Currently, 10 thousand developers worldwide use the programming language, which is rising daily. So, if you're a Java developer, an aspiring...

9 minutes read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.