字符串与字符数组的多种转换方式。

时间:2020-07-13 15:21:37   收藏:0   阅读:57

public class StringAPIDemo1 {
public static void main(String[] args) {
String str = "HELLO";
char c[] = str.toCharArray(); // 将字符串变为字符数组
for (int i = 0; i < c.length; i++) {
System.out.print(c[i] + "\t");
}
System.out.println(); //换行
String str2 = new String(c);// 将整个字符数组转换为字符串
String str3 = new String(c, 0, 3);// 将部分字符数组转换为字符串
System.out.println(str2);
System.out.println(str3);
}
}

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!