Home >Java >JavaBase >How to convert string to char array in java

How to convert string to char array in java

青灯夜游
青灯夜游Original
2021-05-08 11:47:1115785browse

In java, you can use the toCharArray() method to convert a string into a char character array. The syntax format is "String variable.toCharArray()" and the return value is a character array.

How to convert string to char array in java

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

Use .toCharArray() in java to convert a string into a character array

package com.oracle;
 
import java.util.Scanner;
 
public class Test {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		String str=input.next();
		char ss[] = str.toCharArray();//利用toCharArray方法转换
		for (int i = 0; i < ss.length; i++) {
			System.out.println(ss[i]);
		}
			
		
		
	}
 
}

The toCharArray() method converts a string into a character array. Syntax:

public char[] toCharArray()

Parameters

  • None

Return value

  • Characters array.

Recommended related video tutorials: Java video tutorial

The above is the detailed content of How to convert string to char array 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