Java.lang.String.toCharArray() Method

Advertisements


Description

The java.lang.String.toCharArray() method converts this string to a new character array.

Declaration

Following is the declaration for java.lang.String.toCharArray() method

public char[] toCharArray()

Parameters

  • NA

Return Value

This method returns a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

Exception

  • NA

Example

The following example shows the usage of java.lang.String.toCharArray() method.

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {
  
    // converts String value to character array type value  
    String str = " Java was developed by James Gosling";
    char retval[] = str.toCharArray();
    
    // displays the converted value     
    System.out.println("Converted value to character array = ");
    System.out.println(retval);
  }
}

Let us compile and run the above program, this will produce the following result:

Converted value to character array =
Java was developed by James Gosling



Advertisements
Advertisements