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<result.length; x++) {
System.out.println(result[x] + "\n");
}
}
}
The problem seems to be in the character \\a
.
It should be a [A-Za-z].
char[] result = "Stack Me 123 Heppa1 oeu".toCharArray();
You can use
String [] strArr = Str.split("");
I'm pretty sure he doesn't want the spaces to be output though.
for (char c: s.toCharArray()) {
if (isAlpha(c)) {
System.out.println(c);
}
}
Including numbers but not whitespace:
"Stack Me 123 Heppa1 oeu".replaceAll("\\W","").toCharArray();
=> S, t, a, c, k, M, e, 1, 2, 3, H, e, p, p, a, 1, o, e, u
Without numbers and whitespace:
"Stack Me 123 Heppa1 oeu".replaceAll("[^a-z^A-Z]","").toCharArray()
=> S, t, a, c, k, M, e, H, e, p, p, a, o, e, u
"Stack Me 123 Heppa1 oeu".toCharArray()
?
String[] result = "Stack Me 123 Heppa1 oeu".split("**(?<=\\G.{1})**");
System.out.println(java.util.Arrays.toString(result));
Source: Stackoverflow.com