Java Program to Convert String to String Array Using Regular Expression
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
- Using split() method only
- Using ‘?!^’ regular expression along with split() method
Method 1: Using split() method only
Welcome to GFG
Method 2: Using ‘?!^’ regular expression along with split() method
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.
[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.