bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

Java How To Convert a String to an Array

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java How To Convert a String to an Array?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Order

Put the learning moves in the order that makes the concept easiest to apply.

Explanation: The method toCharArray() turns the string into an array of characters.
There are many ways to convert a string to an array.
Convert a String to an Array

Convert a String to an Array

There are many ways to convert a string to an array. The simplest way is to use the toCharArray() method:

char

Explanation: The method toCharArray() turns the string into an array of characters. Each letter of "Hello" becomes one element in the array, so myArray[0] is H .

You can also loop through the array to print all array elements:

Example

// Create a string String myStr = "Hello"; // Convert the string to a char array char[] myArray = myStr.toCharArray(); // Print array elements
for (char i : myArray) {
  System.out.println(i);
}

Explanation: The loop goes through each element in the array and prints it. So for "Hello" , it prints the letters one by one: H, e, l, l, o.

Related Pages

The toCharArray() String Method

Previous

Java How To Check Anagram Strings

Next

Java How To - Remove Whitespace from a String