-5

I'm just wondering if anyone could answer a question for me? Would it be possible to take a string variable of 8 letters and then put each letter into a char array?
For example:

  String str;
  str = "ABCDEFGH";

Would you be able to take this string variable and split it into an array of 8 characters?

Update:
char[] charArray = str.toCharArray();
will do it
Exactly same question, answered here:
Split string into array of character strings

Community
  • 1
  • 1
AndroidCB
  • 230
  • 4
  • 11

4 Answers4

9

How about:

String str = "ABCDEFGH"; 
char[] charArray = str.toCharArray();

Read the documentation for the String class.

keyser
  • 18,829
  • 16
  • 59
  • 101
2

Yes you can, you can use toCharArray() method.

String str = "ABCDEFGH";
char[] array=str.toCharArray();

Please make sure that you read javadoc.

Dulanga
  • 1,326
  • 7
  • 15
1

You already have a method in String class to do so.

String str = "ABCDEFGH";
char[] array=str.toCharArray();
Ankit
  • 1,250
  • 16
  • 23
0
char[] arr=new char[8];
for(int i=0;i<str.length();i++)
{
arr[i]=str.charAt(i);
}
Nizam
  • 5,698
  • 9
  • 45
  • 57
  • Instead of a hard-coded `8`, you should use `str.length()` for the array declaration. Also mention that using the `toCharArray()` method of `String` is preferable. – kevinsa5 Aug 02 '13 at 14:26