What is String?

Strings are a sequence of characters. java strings are considered objects.

The java.lang package contains the String class to create and manipulate strings.

What is Character Array?

char is a primitive data type. A character Array is a group of characters.

Table of Content :

Convert a String to a Character array in Java

We can convert the Given String into the character Array by using the Strings toCharArray() method.

The syntax is

char[] toCharArray()

Converts this string to a new character array.

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

Java program to Convert a String to the Character Array

package com.bala.string; public class ConvertStringToCharArray { public static void main(String[] args) { String value = "HelloWorld"; // String char[] array = value.toCharArray(); // print the char array content by iterating array for (char ch : array) // Iterating array values { System.out.println(ch); } } }

output:

H e l l o W o r l d

Questions related to Convert a String to Character array in Java

How do you convert a string to a char in Java?

We can use the charAt() method of the String class. The charAt() method returns a single character present in the given position.

Can we convert a string to a character array in Java?

By using String class toCharArray() method we can convert a string to a character array.


StringBuilder in Java with Examples
StringBuffer in Java with Examples
String in Java
Java toString() method with Examples

In this article, we have seen Convert a String to Character array in Java.