String→String[]→int[]
public static int[] stringToInt(String str) {
String[] strArray = str.split(",");
int[] a = new int[strArray.length];
for (int i = 0; i < strArray.length; i++) {
a[i] = Integer.parseInt(strArray[i]);
}
return a;
}
String →int
String str2="76"; //此处应注意int的取值范围
int num=Integer.valueOf(str2); //返回值为Integer型,然后自动转换为int型
int num2=Integer.parseInt(str2); //返回值为int型
int→String
int num=567;
String strNum=String.valueOf(num);
char[]→String
char[] str="abcdefg";
String newStr=new String(str);
System.out.println(newStr);
String→char[]
String str="abcdefg";
char[]a=str.toCharArray(); //获取一个字符串的字符数组
String数组→String
- 方法1:借助toString() 方法
String[] arrs=new String[]{"1","2","3","4","5"};
StringBuilder sb=new StringBuilder();
for(String s:arrs){
sb.append(s);
}
String result=sb.toString();
System.out.println(result);
- 方法2:借助for循环
String re1="";
for(int i=0;i<arrs.length;i++){
re1+=arrs[i];
}
System.out.println(re1);
String→String数组
String str="123,65,89,107,230,19,4,91";
String[] str2=str.split(","); //使用split方法切割字符串
for(int i=0;i<str2.length;i++){
System.out.print(str2[i]+" "); //输出新数组str2的值
}