1.字符数组转为字符串
方法1:直接在构造String时转换。
char[] data = {‘a’, ‘b’, ‘c’};
String str = new String(data);
方法2:调用String类的方法转换。
String s =String.valueOf(char[] ch)
2.字符串转为字符数组
情况一:如果是”abcdefghijk”这种字符串,就直接
String string1 = “abcdefghijk” ;
char [] strArr1 = string1.toCharArray(); //注意返回值是char数组
3.字符串字符串数组互转
情况一:如果是有分隔符的那种例如”abc,def,ghi”;就直接分割就行了.
String string = “abc,def,ghi”;
String [] strArr= string.split(“,”); //注意分隔符是需要转译
情况二:无分隔符
String[] arr ={"0123","sb","12f"};StringBuffer sb = new StringBuffer();for(int i = 0;i<arr.length;i++){sb.append(arr[i]); //append String并不拥有该方法,所以借助StringBuffer}String sb1 = sb.toString();
示例
public static void main(String[] args) {// TODO Auto-generated method stub// 1.将字符数组转为字符串// 方法一char[] data = { 'a', 'b', 'c' };// 字符数组String str = new String(data);System.out.println(str);// 方法二String str1 = String.valueOf(data);System.out.println(str1);//字符串转为字符数组// 方法一String string1 = "abcdefghijk";char[] strArr1 = string1.toCharArray();for (int i = 0; i < strArr1.length; i++) {System.out.println(strArr1[i]);}// 将字符串 字符串数组互转// 方法一String string = "abc,def,ghi";String[] strArr = string.split(",");//注意分隔符是需要转译for (int i = 0; i < strArr.length; i++) {System.out.println(strArr[i]);}//方法二String[] arr ={"0123","sb","12f"};StringBuffer sb = new StringBuffer();for(int i = 0;i<arr.length;i++){sb.append(arr[i]); //append String并不拥有该方法,所以借助StringBuffer}String sb1 = sb.toString();System.out.println(sb1); //0123sb12f//字符串逆序String s="123abc";System.out.println(new StringBuilder(s).reverse().toString());
}