Java中字符串转换为字符数组的方法
java string toCharArray() 方法将给定的字符串转换为字符序列。返回的数组长度等于字符串的长度。
public char[] toCharArray()
代码
代码1:
public class Test2 {
public static void main(String[] args) {
String a = "1-1-1-1";
char[] temp = a.toCharArray();
//System.out.println(temp.getClass().toString());//class [C char的数组类型
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
}
}
}
代码2:
public class Test2 {
public static void main(String[] args) {
String a = "1-1-1-1";
for (char c: a.toCharArray()) {
System.out.println(c);
}
}
}
结果
代码1和代码2的结果相同
1
-
1
-
1
-
1