简体   繁体   English

如何将字符串拆分为数组长度?

[英]How do I split a string into an array the length?

I'm fairly new to Java but I would like to split a string up into characters and store them in an array then calculate the total characters. 我对Java还是很陌生,但是我想将字符串拆分为字符并将它们存储在数组中,然后计算字符总数。 This is my code so far. 到目前为止这是我的代码。

public class Main{ 公共类Main {

public static void main(String []args){
String Example = "This is an example";      //String 
String[] array = Example.split(",");
int totalLength = array.length();
System.out.println(totalLength);

}

} }

You could use String.toCharArray() and something like 你可以使用String.toCharArray()东西

String example = "This is an example";      //String 
char[] array = example.toCharArray();

int totalLength = array.length;
System.out.println(totalLength);

You could do 你可以做到

char[] array = "This is an example".toCharArray();

Use the array length property to get the length 使用数组length属性来获取长度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM