×

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 Maven Silicon

Maven is a build automation tool that is mostly used for Java applications. It allows you to manage the build, reporting, and documentation of a project from the central location. Maven provides...

4 minutes read.

Java Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

Java Enum vs Class

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four integrators named Club, Diamond,...

7 minutes read.

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Java Single Linkedlist

Like arrays, linked lists are a type of linear data structure. Unlike arrays, which store each element in the same location, linked lists employ pointers to connect the elements together. In...

25 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.