×

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

Java Viva Questions and Answers

Question: Explain Java programming language. Answer: It is a high-level programming language. This programming language is based on the principles of object-oriented programming. Large-scale applications can be developed by using this...

16 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.

Java Case Keyword

Case keyword: In this article we are going to learn the concept of a java case keyword.Generally, java case keyword is used with the switch statements.Case keyword is implemented in conditional...

3 minutes read.

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.

Dangling Else problem in Java

A language interpretation uncertainty is the hanging other issue. The following two types of condition executed code are both possible in programming: 1. if-then-else form 2. if-then form When dealing with the nested...

3 minutes read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

Java Integer divideUnsigned() method

The divideUnsigned() method of Integer class returns the unsigned quotient by dividing the first argument by the second argument. Syntax public static int divideUnsigned (int dividend , int divisor) Parameters The parameter ‘dividend’ represents...

1 minute read.

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Logger class in Java

Logging is a crucial component of Java that aids developers in tracking down mistakes. The logging technique is included with the computer language Java. The possibility of collect the log...

7 minutes read.

Creating a Custom Generic Class in Java

To indicate parameter types when creating generic classes, we utilize the <> symbol. The syntax used to generate objects of a generic class is as follows. // To create an instance...

4 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Parallel Arrays Sort in Java

Sorting is a technique of arranging a sequence of numbers in ascending order, that is, the first number being lowest and gradually incrementing the numbers such that the last number...

7 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

How to install Java in Windows 10

To make programs that can run on our systems, we need to install the programming language related software in our systems. Different programming language requires a different type of software aka...

6 minutes read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.