BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Split String by Multiple Delimiters in Java

By Chaitanya Singh | Filed Under: Java Examples

We learned various ways to split string in Java. In this guide, we will see how to split string by multiple delimiters in Java.

Program to Split String by Multiple Delimiters

In this example, we have a string that contains multiple special characters, we want to split this string using these special characters as delimiters. We can do this by using regex, all the delimiters are specified inside brackets “[ ]“. This regex is used inside split method of Java String class.

public class JavaExample
{
  public static void main (String[] args)
  {
    //A string with multiple delimiters
    String str = "Text1,Text2#Text3 Text4:Text5.Text6";

    //Specified the delimiters inside brackets, there is a
    //whitespace after # to consider space as delimiter as well
    String[] strArray = str.split("[,# :.]");

    for(String s: strArray){
      System.out.println(s);
    }
  }
}

Output:

Text1
Text2
Text3
Text4
Text5
Text6

Multiple consecutive delimiters in a String

The above program is fine if the given string doesn’t contain the multiple consecutive delimiters, however if it does then the above solution would produce empty string, see the following program:

public class JavaExample
{
  public static void main (String[] args)
  {
    //A string with multiple consecutive delimiters
    String str = "Text1,##::Text2";

    //Specified the delimiters inside brackets
    String[] strArray = str.split("[,#:]");

    for(String s: strArray){
      System.out.println(s);
    }
  }
}

Output: As you can see we have empty strings in the output, if you do not want this behaviour then see the next program where we have solution for this:

Split String by multiple delimiters example output

To avoid empty string use the regex shown in the following program:

public class JavaExample
{
  public static void main (String[] args)
  {
    String str = "Text1,##::Text2";
    //there is a + sign after closing bracket, this is
    //to consider consecutive delimiters as one
    String[] strArray = str.split("[,#:]+");
    for(String s: strArray){
      System.out.println(s);
    }
  }
}

Output:

Split String example output

Related Programs:

  • Split String by comma
  • Split String by space
  • Split string by capitalLetters
  • Split string by pipe character

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap