Home >Java >javaTutorial >Usage of tochararray in java

Usage of tochararray in java

下次还敢
下次还敢Original
2024-05-01 18:45:43932browse

The toCharArray() method in Java converts a string into a character array. It receives a string as argument and returns a character array containing the characters in the string.

Usage of tochararray in java

##In JavatoCharArray() Usage

toCharArray() is a method of the String class in Java, used to convert a string into a character array.

Usage:

<code class="java">char[] charArray = string.toCharArray();</code>
Among them,

string is the string to be converted, charArray is the converted character array .

Example:

<code class="java">String str = "Hello World";
char[] charArray = str.toCharArray();

System.out.println(Arrays.toString(charArray));</code>
Output:

<code>[H, e, l, l, o,  , W, o, r, l, d]</code>

Note:

    in character array Each element of is the Unicode value of the corresponding character in the string.
  • The original string will not be modified.
  • If the string is
  • null, a NullPointerException exception will be thrown.

The above is the detailed content of Usage of tochararray in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn