0

Given a String, say String s = "abcderfh";

How would you turn this into a String Array.

Thank you in Advance.....

12
  • I guess that you mean a "char" array instead of a String array since you give an example with a single word. Commented Nov 3, 2011 at 5:47
  • unless he want's a string array. I know it sounds crazy... Commented Nov 3, 2011 at 5:47
  • A String array containing what? The individual characters? An array with just one element, that string? Something else? Commented Nov 3, 2011 at 5:49
  • Yeah definitely, Jeetesh, could you precise what you meant? Commented Nov 3, 2011 at 5:50
  • @Laurence : An array of string objects that contain single letters. A single letter can be a string. Commented Nov 3, 2011 at 5:52

3 Answers 3

5

If you would like to obtain an array of characters, you should use the toCharArray() method on the String.

So you can do this:

String s = "abcderfh";
char[] myarray = s.toCharArray()
Sign up to request clarification or add additional context in comments.

Comments

2

If you mean an array of String, where each element is one letter, do this:

String s = "abcderfh";
String[] letters = s.split("(?<=.)"); // split after every character
System.out.println(Arrays.toString(letters));

Output:

[a, b, c, d, e, r, f, h]

1 Comment

Why the downvote? This is a valid answer given the vague question.
-2

You can find a great example right here:

http://api.jquery.com/jQuery.makeArray/

1 Comment

It is not at all about JQuery, it is a Java question.

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.