Table of contents
1.
Introduction
2.
String toCharArray() Function in Java
2.1.
Syntax
2.2.
Parameter
2.3.
Return Value
2.4.
Internal Implementation
2.5.
Examples
3.
Implementation
4.
Frequently Asked Questions
4.1.
What is the use of character array in Java?
4.2.
How to reverse a string in Java using toCharArray?
4.3.
What is the use of charAt() method?
5.
Conclusion
Last Updated: Nov 10, 2024

String toCharArray() in Java

Author ANJU JAISWAL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java's String class provides several methods for manipulating text, and one such method is toCharArray(). This method allows you to convert a string into an array of characters. It can be useful when you need to process or manipulate individual characters from a string. 

”tochararray java

In this article, we will discuss how to use the toCharArray() method in Java, covering its syntax, parameters, return value, and providing examples for better understanding.

String toCharArray() Function in Java

The toCharArray() method in Java is a built-in function that converts a string to a sequence of characters. The length of the output array is equal to the length of the String, and the chars in Array are in the same order as the characters in the String.

Syntax

The syntax of string toCharArray() method is given below:

string.toCharArray()
You can also try this code with Online Java Compiler
Run Code

 

The string is a String class object in this case.

Parameter

The toCharArray() method doesn't take any parameters.

Return Value

It returns a newly allocated character array.

Internal Implementation

public char[] toCharArray() {  
      char output[] = new char[value.length];  
      System.arraycopy(value, 0, output, 0, value.length);  
      return output;  
  }
You can also try this code with Online Java Compiler
Run Code

Examples

Examples

Input: hello !

Output: hello !

Implementation

import java.util.*;
import java.io.*;
import java.lang.*;


class CodingNinjas
{
    public static void main (String[] args) throws Java.lang.Exception
    {
      String str = new String("Hello !");
/*String toCharArray() converts a string to a sequence of characters */

      char[] arr = str.toCharArray();

      System.out.print("Return Value : " );
      System.out.println(str.toCharArray() );
      System.out.println("\nSize of char[] = " + arr.length);
  

    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Return Value : Hello !

Size of char[] = 7
You can also try this code with Online Java Compiler
Run Code

 

Check out this problem - Shortest Common Supersequence.

Must Read Array of Objects in Java

Frequently Asked Questions

What is the use of character array in Java?

A character array in Java is used to store a sequence of characters. It allows manipulation of each character individually, making it useful for tasks like string processing, searching, or modifying specific characters.

How to reverse a string in Java using toCharArray?

To reverse a string using toCharArray(), first convert the string to a character array using toCharArray(). Then, reverse the array and create a new string from the reversed array.

What is the use of charAt() method?

The charAt() method in Java is used to get the character at a specific index in a string. It helps in accessing individual characters for string manipulation or searching.

Conclusion

This article taught us about String toCharArray() in Java and its implementation. The Java String toCharArray() method converts the String to a char array and returns it. We have also seen some examples to understand better how this method works.

Recommended Readings:

Recommended problems -

 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine learning and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Live masterclass