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.

2
  • Possible duplicate of Converting String to “Character” array in Java Commented Feb 24, 2014 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. Commented Feb 24, 2014 at 15:03

4 Answers 4

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'}
Sign up to request clarification or add additional context in comments.

Comments

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
}

1 Comment

On a side note, please try going through the API's and some tutorials it will help in improving your understanding of the language
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();

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.