Split a String Using Regular Expression in Java



Let’s say we have the following string.

String str = "{Java is a programming language} {James Gosling developed it.}";

Above, the string is enclosed with parentheses. We can split a string with these parentheses using the split() method.

String[] strSplit = str.split("[{}]");

The following is an example.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) throws Exception {
      String str = "{Java is a programming language} {James Gosling developed it.}";
      String[] strSplit = str.split("[{}]");
      System.out.println("Splitting String...");
      for (int i = 0; i < strSplit.length; i++)
      System.out.println(strSplit[i]);
   }
}

Output

Splitting String...
Java is a programming language
James Gosling developed it.
Updated on: 2020-06-27T06:51:09+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements