×

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

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about...

4 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.

Static and Dynamic Binding in Java

Data Binding in Java The relation between a class and method or class and field is known as Data Binding. In Java, we can create a class for Student_info, but until we...

2 minutes read.

Java Math ulp() Method

The ulp() method of Java Math class returns the size of an ulp of the argument. Syntax: public static double ulp(double x)public static float ulp(float x) Parameters: The parameter ‘x’ represents the floating-point number...

2 minutes read.

Union in Java

Sets.union() method in Java returns an immutable representation of both the union of two sets. Every element that is present in either backup set is included in the set that...

2 minutes read.

Caesar Cipher Program in Java

It is among the most popular and straightforward encryption methods. With this method, each letter in the text is swapped out for a letter that is a certain number of...

3 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

Jagged Array in Java

Prerequisite We must first understand what Arrays are and Multi-dimensional Arrays are before we can learn about Jagged arrays. Java Arrays: A collection of data types that are similar is called an...

5 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Prime Number Program in Java Using a Scanner

In Java, a prime number is one that can only be divided by one or by itself and is greater than one. In other words, only one or itself can...

3 minutes read.

Concurrent Modification Exception In Java

When an object is attempted to be updated concurrently when it is not allowed, the ConcurrentModificationException arises. This error typically occurs while using Java Collection classes. When another thread is iterating...

5 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

Java Control Statements

Control Statements: Control statements in Java can also be referred to as decision-making while dealing with different problems. Control statements are helpful to sort out the flow of the program or...

7 minutes read.

Kong Java Client

Kong is an Organization Microservice Programming interface gateway. Kong gives an adaptable deliberation layer that safely oversees correspondence among clients and microservices by means of a Programming interface. Otherwise called...

6 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.