Open In App

Java Program to Convert String to String Array Using Regular Expression

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are few areas of strings where Regex is widely used to define the constraints. Regular Expressions are provided under java. util.regex package. This consists of 3 classes and 1 interface. Hence, in short, we can conclude, java’s regular expression is used to find, match, and extract data from character sequences. 

Approach: We can convert String to String Array using the split method of the String class and Regular Expression. 

  1. First, we split the string with the help of Regular Expression
  2. Now store this in an array.
  3. Print and display on the console

Methods: 

  1. Using split() method only
  2. Using ‘?!^’ regular expression along with split() method

Method 1: Using split() method only

Example:

Java




// Java program to demonstrate how to convert String to
// String Array using Regular Expression in Java
 
// Importing all classes from
// java.util package
import java.util.*;
 
// Importing Matcher class that searches through a text for
// multiple occurrences of a regular expression 
import java.util.regex.Matcher;
 
// Importing Pattern class
import java.util.regex.Pattern;
 
// Importing Pattern class to compile regex
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Random string input
        String gfg = "Welcome, to, GFG";
 
        // Splitting of string into characters and
        // storing it in an array string
        // separated by comma in between characters
        String[] str = gfg.split(",");
 
        // Traversing the above array
        // using for each loop
        for (String s : str) {
 
            // Print the characters of the array
            // formed from input string
            System.out.println(s);
        }
    }
}


Output

Welcome
 to
 GFG

Method 2: Using ‘?!^’ regular expression along with split() method

Approach: 

With the split method, we will use the ?!^ Regular Expression that split the string. And extract data of character sequence.  

  • ?! — It means a negative look ahead.
  • ^ — It is the start of the string.

Example: 

Java




// Java program to demonstrate how to convert String to
// String Array using Regular Expression in Java
 
// Importing all classes from
// java.util package
import java.util.*;
 
// Importing Matcher and Pattern classes from
// java.util.regex package because
 
// Matcher Class searches through a text for
// multiple occurrences of a regular expression
import java.util.regex.Matcher;
 
// Pattern Class to compile regex
import java.util.regex.Pattern;
 
// Class
class GFG {
   
    // Main driver method
    public static void main(String[] args)
    {
        // Random input string
        String gfg = "Welcome, to, GFG";
 
        // Split the above input string
        // using split() method and
        // store the input string elements as an array
         String[] str = gfg.split("(?!^)");
 
        // Print all the elements of an array
        System.out.println(Arrays.toString(str));
    }
}


Output

[W, e, l, c, o, m, e, ,,  , t, o, ,,  , G, F, G]

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads