Jeetesh Nataraj
Jeetesh Nataraj

Reputation: 608

How to turn a given String into a Array?

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

How would you turn this into a String Array.

Thank you in Advance.....

Upvotes: 0

Views: 119

Answers (3)

lc2817
lc2817

Reputation: 3742

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()

Upvotes: 5

Bohemian
Bohemian

Reputation: 425398

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]

Upvotes: 2

casper60
casper60

Reputation: 88

You can find a great example right here:

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

Upvotes: -2

Related Questions