Used for each data type conversion - Summary 1 (char [] turn String)

CharArrayToString Summary

public class CharArrayToString {
public static void main (String [] args) {
char [] CH = { 'in', 'Chinese', 'person', 'public' 'total' 'taken', 'State'};
@ method a: () method, the array elements in StringBuilder append traverse through and connect
// Note that, in the storage space StringBuilder heap created
// Create a StringBuilder object.
StringBuilder StringBuilder new new STR = ();
// iterate through the elements of the array, and connecting them together
for (int I = 0; I <ch.length; I ++) {
. str.append (CH [I]) the append (I == . 1-ch.length?. "": "");
}
// print result
System.out.println ( "str value is:" + str);

    System.out.println("---------------------------------------");

    //方法二:通过String进行字符串拼接
    //注意,String数据也是存储在方法区的常量池中,也就意味着这种方式比StringBuilder耗费更多的内存
    String str2="";
    for(int i=0;i<ch.length;i++){
        str2+=ch[i];
    }
    System.out.println("str2的值是:"+str2);


    System.out.println("----------------------------------");

    //方法三:通过String下的Valueof()方法
    String str3="";
    str3=String.valueOf(ch);
    System.out.println("str3的值是:"+str3);


    //方法四:通过String下的构造方法,直接返回String字符串
    System.out.println("------------------------------------");
    String str4=new String(ch);
    System.out.println("str4的值是:"+str4);

}

}

Released three original articles · won praise 0 · Views 117

Guess you like

Origin blog.csdn.net/luoyanlin0021/article/details/104733409