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.
4 Answers
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
}
1 Comment
Kakarot
On a side note, please try going through the API's and some tutorials it will help in improving your understanding of the language
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();
"ABCD".split("")- This won't give you an array ofchar, instead you get an array ofString, 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.