字符串-->数组
① 转换成String类型的数组
String string = "abc,def,ghi";
String[] stringArray = string.split("\\,");
// java中通常用split()分割字符串,返回的是一个数组
for(String a : stringArray){
System.out.println(a);
}
② 转换成char类型的数组
String string = "abc,def,ghi";
char[] stringArray = string.toCharArray();
for(char a : stringArray){
System.out.println(a);
}
③ 转换成byte类型的数组
String string = "abc,def,ghi";
byte[] stringArray = string.getBytes();
for(byte a : stringArray){
System.out.println(a);
}
数组-->字符串
① char类型的数组转换成字符串
char[] array = {‘1‘,‘2‘,‘3‘,‘4‘};
String a = String.copyValueOf(array);
System.out.println(a);
② String类型的数组转换成字符串
String[] strArray = {"1er","2df","3sw","4ui"};
StringBuffer strBuffer = new StringBuffer();
for(int i=0;i<strArray.length;i++){
strBuffer.append(strArray[i]);
}
String b = strBuffer.toString();
System.out.println(b);
原文地址:https://www.cnblogs.com/twtBlogs/p/10528502.html