×

Java String toCharArray() method

Java String toCharArray() method converts current String into character array.

Syntax:

public char[] toCharArray()

Returns:

It returns a character array

Java String toCharArray() method example 1

import java.util.Arrays;
public class JavaStringToCharArrayEx1
{
          public static void main(String[] args)
          {
                   // String declaration
                   String stringValue = "Java tutorial and example";
                   // Convert String to char array
                   char[] charArray = stringValue.toCharArray();
                   // array printing of character array
                   System.out.println(Arrays.toString(charArray));
          }
}

Output:

[J, a, v, a,  , t, u, t, o, r, i, a, l,  , a, n, d,  , e, x, a, m, p, l, e]

Java String toCharArray() Method Example 2

public class JavaStringToCharArrayEx2
{ 
public static void main(String args[])
{ 
        String s1="Java tutorial and example"; 
         char[] ch=s1.toCharArray(); 
for(int i=0;i<ch.length;i++)
{ 
System.out.print(ch[i]); 
} 
}     
}

Output:

Java tutorial and example

Java String toCharArray() Method Example 2

public class JavaStringToCharArrayEx3
{ 
    public static void main(String[] args)
    { 
        String s1 = "Tutorial And Examples"; 
        char[] ch = s1.toCharArray(); 
        int len = ch.length; 
        System.out.println("Char Array length: " + len); 
        System.out.println("Char Array elements: "); 
        for (int i = 0; i < len; i++) { 
            System.out.println(ch[i]); 
        } 
    } 
}

Output:

Char Array length: 21
Char Array elements:


T
u
t
o
r
i
a
l


A
n
d


E
x
a
m
p
l
e
s

Java String toCharArray() Method Example 3

public class JavaStringToCharJavaEx3 {
       public static void main(String[] args) {
              String str = "Java tutorial and example";             
              //string to char array
              char[] chars = str.toCharArray();
              System.out.println("Char Array Length :" +(chars.length));
              System.out.println();
              //char at specific index
              char c = str.charAt(0);
              char c1 = str.charAt(1);
              char c2 = str.charAt(2);
        char c3 = str.charAt(3);   
              System.out.println(c);
              System.out.println(c1);
              System.out.println(c2);
              System.out.println(c3);
              System.out.println();        
              // string characters to char array
              char[] chars1 = new char[7];
              str.getChars(0, 7, chars1, 0);
              System.out.println(chars1);           
       }
}

Output:

Char Array Length :25

J
a
v
a
Java tu

Java String toCharArray() Method Example 4

public class JavaStringToCharJavaEx4
{
        public static void main(String[] args)
    {
        String str = "Java Tutorial by Roy NK";
        // Convert the above string to a char array.
            char[] arr = str.toCharArray();
            System.out.println();
        // Display the contents of the char array.
            System.out.println(arr);
            System.out.println();
    }
}

Output:

Java Tutorial by Roy NK

Related Topics

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Upcasting in Java

To know what is Upcasting in Java we should first equip ourselves about what is casting or type casting in Java. Casting The process of providing or replacing a reference variable of...

3 minutes read.

Java Math subtractExact() Method

The subtractExact() method of Java Math class returns mathematical difference of the specified two arguments, throwing an exception if the result overflows int or long. Syntax: public static int subtractExact (int x,...

1 minute read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

POJO in Java

Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java...

3 minutes read.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

Java Try Resource

In Java, a try argument that specifies one or more resources is known as a try-with-resources statement. When your program is finished utilizing an object, it must be closed, which...

4 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.