Skip to content
Related Articles

Related Articles

Java Program to Convert String to String Array Using Regular Expression
  • Last Updated : 09 Mar, 2021

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 classses from
// java.util package
import java.util.*;
  
// Importing Matcher class that searches through a text for
// multiple occurences 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
        // spearated 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 occurences 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]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :