简体   繁体   中英

How to convert char[] to string?

I have a question in this case how to convert char[] to string?(Must explicitly convert the char[] to a String) sorry being a novice programmer i am asking this.

 String str3 = "Dad saw I was Playing";
        System.out.println(str3.toCharArray() + "\n" + str3.toLowerCase() );
        System.out.println(str3.toCharArray());

Output: [C@1034bb5 //this is what I am getting for first str3.tochararray(),how to resolve this?
dad saw i was playing
Dad saw I was Playing

You can use Arrays.toString(char[]) (or create your own method if you prefer a different formatting). Something like,

String str3 = "Dad saw I was Playing";
System.out.println(Arrays.toString(str3.toCharArray()));

System.out.println(str3.toCharArray() + "\\n" + str3.toLowerCase() );

This first calls toString over CharArray which outputs [C@1034bb5 and then append it to the rest of the string and print.

System.out.println(str3.toCharArray()); -- This calls the overridden method in PrintStream class which can interpret char array and print it.

You can explicitly convert your char array to string and print.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM