24

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].

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

7 Answers7

56

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.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • +1: Hmm, I didn't think about creating another copy of the string. Good point. – Powerlord Oct 05 '09 at 19:34
  • 1
    One thing to consider here is str.charAt(i) will return a char. So if you need a String you will need to do Character.toString(str.charAt(i)). This will create a copy of that character, but that is still better than O(n) space that using str.split("") would give you. – catalyst294 Feb 09 '14 at 16:51
  • 1
    this solution will give array of length 1 if input is empty string – rightful Jul 26 '20 at 19:53
  • The last comment is correct: An empty string is the **only** case where ```.split("")``` would add an extra (empty) element to the array. – Rautermann May 22 '21 at 21:30
30

"Stack Me 123 Heppa1 oeu".toCharArray() ?

Zed
  • 57,028
  • 9
  • 76
  • 100
  • 1
    +1: Simple, concise, and can easily be fed to for. – Powerlord Oct 05 '09 at 19:33
  • 1
    Creating an extra char array is an extra step that is not necessary. – jjnguy Oct 05 '09 at 19:37
  • Depends. My _guess_ is that above a certain length an arrayCopy and direct array access is "faster" than calling hundreds of charAt's. – Zed Oct 05 '09 at 19:41
  • 2
    Calling `charAt` is O(1) because the String is implemented using an array in the back end. Creating a copy will be slower always. – jjnguy Oct 05 '09 at 19:46
  • 1
    Calling `charAt(i)` is implemented to first make a sanity check on the input parameter then offsetting it with the internal offset, and after this yes, it is an array access. – Zed Oct 05 '09 at 19:55
8

You can use

String [] strArr = Str.split("");
Tunaki
  • 132,869
  • 46
  • 340
  • 423
6

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

p3t0r
  • 1,980
  • 1
  • 16
  • 22
3
 char[] result = "Stack Me 123 Heppa1 oeu".toCharArray();
jjnguy
  • 136,852
  • 53
  • 295
  • 323
David
  • 19,389
  • 12
  • 63
  • 87
2

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);
     }
}
Gandalf
  • 9,648
  • 8
  • 53
  • 88
0
String[] result = "Stack Me 123 Heppa1 oeu".split("**(?<=\\G.{1})**");
System.out.println(java.util.Arrays.toString(result));
idursun
  • 6,261
  • 1
  • 37
  • 51
  • Your code thrown below error when executed. `Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0 **(?<=\G.{1})** ^ at java.util.regex.Pattern.error(Pattern.java:1924) at java.util.regex.Pattern.sequence(Pattern.java:2090) at java.util.regex.Pattern.expr(Pattern.java:1964) at java.util.regex.Pattern.compile(Pattern.java:1665) at java.util.regex.Pattern.(Pattern.java:1337) at java.util.regex.Pattern.compile(Pattern.java:1022) at java.lang.String.split(String.java:2313) at java.lang.String.split(String.java:2355)` – Suresh Nov 03 '17 at 06:42