Create Character Array from String Objects in Java



To create a character array from a string object, we convert each character of the string into elements of an array of type char. This is useful when we need to manipulate or access individual characters within the string.

String Object: A string object is an instance of the String class representing a sequence of characters. It is immutable, which means once it is created, its value cannot be changed.

Character Array: A character array is a data structure in Java that stores a sequence of characters.

Creating Character Arrays from String Objects in Java is quite easy. Let's learn the following methods:

Using String toCharArray() Method

To Create a character array from a string object. we have a method named toCharArray() method which is a member of the String class that belongs to java.lang package.

It is used to convert a string into an array of characters. This method creates a new character array and copies the characters from the string into the array.

Example

In the following example, we convert a string into an array of characters using the String toCharArray() Method.

public class Demo {
   public static void main(String[] args) {
      String str = "Hello, World!";
      char[] charArray = str.toCharArray();
      // Print the character array
      for (char c : charArray) {
         System.out.println(c + "");
      }
   }
}

Output

The above program produce the following result ?

H
e
l
l
o
,
 
W
o
r
l
d
!

Using String.getChars() Method

To Create a character array from a string object. we have another method named String.getChars() method which belongs to String class of java.lang package.

It is used to copy a specific character from a string into a character array, starting at a specified position.

Example

Another example is used to copy specific characters at a specified position using the String.getChars() method.

public class Demo {
   public static void main(String[] args) {
      String str = "Hello, World!";
      char[] charArray = new char[str.length()];
      str.getChars(0, str.length(), charArray, 0);
      // Print the character array
      for (char c : charArray) {
         System.out.println(c + "");
      }
   }
}

Output

The above program produce the following result ?

H
e
l
l
o
,
 
W
o
r
l
d
!

Using String.charAt() Method

To Create a character array from a string object. we have one more method named String.charAt() Method. The charAt() method is a function of the String class in Java belongs to java.lang package.

It is used to retrieve the character located at a specific index within a string.

Example

In this example, we retrieve the character located at a specific index using String.charAt() method.

public class Demo {
   public static void main(String[] args) {
      String str = "Hello, World!";
      char[] charArray = new char[str.length()];
      for (int i = 0; i < str.length(); i++) {
          charArray[i] = str.charAt(i);
      }
      // Print the character array
      for (char c : charArray) {
          System.out.println(c + "");
      }
   }
}

Output

The above program produce the following result ?

H
e
l
l
o
,
 
W
o
r
l
d
!
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-01-14T03:34:28+05:30

300 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements