How to convert string to string array using regular expression in java

Regular Expressions, or Regex (for short), are APIs for defining string patterns that can be used to find, manipulate, and manipulate strings in Java. Email and password validation are some of the string ranges that regex often use to set limits. Regular expressions are provided under Java. util.regex package. It consists of 3 classes and 1 interface. So, in summary, we can conclude that Java regular expressions are used to find, match, and retrieve data from strings.

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 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 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
		// 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 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]

 

Submit Your Programming Assignment Details