0

I'm just new in Java and I would like to ask if it's possible to separate each letter of a word. For example, my input is "ABCD" and I would like to display each letter of that word. So, the expected output should be. The input word consists of letters: "A", "B", "C", "D". Sorry, I am just very new in Java and I would like to know if this is possible.

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36
  • Possible duplicate of [Converting String to “Character” array in Java](http://stackoverflow.com/questions/10006165/converting-string-to-character-array-in-java) – Suzon Feb 24 '14 at 14:57
  • `"ABCD".split("")` - This won't give you an array of `char`, instead you get an array of `String`, but the first and last elements in the array are the empty string. Depending on the application, the fact your letters start at index 1 and the last one is an empty string can be nice things while processing. – Dan Temple Feb 24 '14 at 15:03

4 Answers4

2
String.toCharArray()

is the method you are looking for

It will create an Array of Chars. So your string would become

{'A','B','C','D'}
Mason T.
  • 1,567
  • 1
  • 14
  • 33
2

Its possible to split your String into individual characters. Below is the method to do it :

String str = "ABCD";

// this will create Array of all chars in the String
char[] chars = str.toCharArray(); 

// Now loop through the char array and perform the desired operations
for(char val : chars)
{
  // do something
  // variable val will have individual characters
}
Kakarot
  • 4,252
  • 2
  • 16
  • 18
  • On a side note, please try going through the API's and some tutorials it will help in improving your understanding of the language – Kakarot Feb 24 '14 at 14:57
1

Yes, you use the String.toCharArray() method ( here's the API ).

Learn more here - String to char array Java

example:

String happy = "Yaya happee";
char[] happier = happy.toCharArray();
Community
  • 1
  • 1
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
0

You can use String methods. (length() and charAt(int index)) I think you are new at programming so using String methods are better for you if you do not know arrays.

dijkstra
  • 1,068
  • 2
  • 16
  • 39