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

How to convert string to char array in java

青灯夜游
Release: 2023-01-13 00:39:55
Original
15774 people have browsed it

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]);
		}
			
		
		
	}
 
}
Copy after login

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

public char[] toCharArray()
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template