How to Split a Java String Into Characters
- 1). Launch your Java editor, and open one of your Java projects.
- 2). Locate the project's "Main" method, and add the following code to the beginning of that method:
String inputString = "abcd";
char[] characterArray = inputString.toCharArray();
for(int i=0; i<characterArray.length; i++) {
char character = characterArray[i];
System.out.println("Array Element " + i + " " + character);
}
Note the variable named "inputString." This is the string you wish to split. Replace "abcd" with any text you like. The next statement defines a character array that holds the characters extracted from the string. The "toCharArray" method splits the string. The final lines of code loop through the characters in that array, and show you their values and positions within the array. - 3). Save your project and run it. The code converts the input string to a character array and shows you the characters in the character array.
Source...