How can you split a word to its constituent letters?
Example of code which is not working
class Test {
public static void main( String[] args) {
String[] result = "Stack Me 123 Heppa1 oeu".split("\\a");
// output should be
// S
// t
// a
// c
// k
// M
// e
// H
// e
// ...
for ( int x=0; x
System.out.println(result[x] + "\n");
}
}
}
The problem seems to be in the character \\a.
It should be a [A-Za-z].
解决方案
You need to use split("");.
That will split it by every character.
However I think it would be better to iterate over a String's characters like so:
for (int i = 0;i < str.length(); i++){
System.out.println(str.charAt(i));
}
It is unnecessary to create another copy of your String in a different form.