×

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 Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Bouncy Number in Java

We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the...

3 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Java Char Keyword

The java char keyword is a data type which is defined as character data type.Char keyword belongs to a primitive data type where the data types are classified into primitive...

4 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

4 minutes read.

Java Worker Class

What is a Worker? A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker...

3 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

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

2 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.

Powerful Number in Java

We will define a powerful number in this article and write Java programs to determine whether a given number is a powerful number or not. Java coding interviews and academic...

6 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.