In this guide, we will see how to split a string into array of characters in Java. This can be archived by using this regex (?!^)
in the split method of Java string class.
Java Program to Split String into Array of Characters
Explanation of regex ( ? ! ^ ):
The ?! part in this regex is negative assertion, which it works like a not operator in the context of regular expression. The ^ is to match the beginning of the string. Together it matches any character that is not the beginning of the string, which means it splits the string on every character.
public class JavaExample { public static void main (String[] args) { String str = "BeginnersBook.com"; //This will split a string into individual characters String[] strArray = str.split("(?!^)"); //print array of characters for(String s: strArray){ System.out.println(s); } } }
Output:
Related guides: