Java Program to Convert String to String Array Using Regular Expression
Last Updated :
02 Jun, 2022
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.
- First, we split the string with the help of Regular Expression
- Now store this in an array.
- Print and display on the console
Methods:
- Using split() method only
- 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);
}
}
}
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]
Similar Reads
Java Program to Convert String to String Array Given a String, the task is to convert the string into an Array of Strings in Java. It is illustrated in the below illustration which is as follows: Illustration: Input: String : "Geeks for Geeks" Output: String[]: [Geeks for Geeks]Input: String : "A computer science portal" Output: String[] : [A co
6 min read
Java Program to Convert String to Char Stream Without Using Stream Char stream defines the array of characters. In this article, we will learn the different types of methods for converting a String into a char stream in Java without using Stream. Let us see some methods one by one. ExamplesInput: String = HelloGeeksOutput: [H, e, l, l, o, G, e, e, k, s] Input: Stri
4 min read
Java Program to Convert String to Integer Array In Java, we cannot directly perform numeric operations on a String representing numbers. To handle numeric values, we first need to convert the string into an integer array. In this article, we will discuss different methods for converting a numeric string to an integer array in Java.Example:Below i
3 min read
Java Program to Extract an HTML Tag from a String using RegEx In this article, we will find/extract an HTML tag from a string with help of regular expressions. The Regular Expression Regex or Rational Expression is simply a character sequence that specifies a search pattern in a particular text. It can contain a single character or it can have a complex sequen
3 min read
How to Split Strings Using Regular Expressions in Java? Split a String while passing a regular expression (Regex) in the argument and a single String will split based on (Regex), as a result, we can store the string on the Array of strings. In this article, we will learn how to split the string based on the given regular expression. Example of Split Stri
2 min read
Java Program to Extract a Single Quote Enclosed String From a Larger String using Regex Problem Statement: Given a String extract the substring enclosed in single quotes (') using Java Regex. Java regex is an API for pattern matching with regular expression. 'java.util.regex' is a class used for CharSequence< interface in order to support matching against characters from a wide vari
3 min read