×

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

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Java Generics Questions

Introduction We'll walk through a few real-world examples of interview questions and responses for Java generics in this article. Java 5 saw the debut of the fundamental idea of generics. Due to...

9 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel...

3 minutes read.

Differences between Lock and Monitor in Java Concurrency

In this tutorial, we will discuss the overview of Lock and Monitor and the differences between them. Introduction Java Concurrency is the ability to perform specific tasks at a time parallelly. The...

4 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

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

Map of Map in Java

The map is now a Java interface for mapping keys to values. It is frequently necessary to use Map of Map (nested Map). Nested Maps are useful in various situations, including...

3 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Java Polymorphism

The process of representing one form in multiple forms is known as Polymorphism. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means...

5 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

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

Difference between String Tokenizer and split Method in Java

Introduction Today, let us understand the difference between String Tokenizer and Split Method. First, let us learn about the String Tokenizer and Split method individually and then know about their differences String...

7 minutes read.