Home >Java >javaTutorial >How to use tochararray in java
The toCharArray() method converts a string into a character array containing each of its characters. It returns a new array containing a copy of each character in the string. Usage: Call the toCharArray() method on a string object. Note that this method does not modify the original string, and the characters in the character array are Unicode code points.
##toCharArray() method
toCharArray() method Is a method of the
String class in Java, which converts a string into a character array. It returns a new character array containing a copy of each character in the string.
Syntax
<code>public char[] toCharArray()</code>
Return Value
A character array containing a copy of each character in the string.Usage
To use thetoCharArray() method, just call it on a string object. For example:
<code>String str = "Hello World"; char[] chars = str.toCharArray();</code>
chars is now a character array containing each character in "Hello World".
Note
method returns a new array, it does not modify the original string.
<code>System.out.println(chars[0]); // 输出 'H'</code>
The above is the detailed content of How to use tochararray in java. For more information, please follow other related articles on the PHP Chinese website!