
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy Char Array to String in Java
Use the valueOf() method in Java to copy char array to string. You can also use the copyValueOf() method, which represents the character sequence in the array specified. Here, you can specify the part of array to be copied.
Let us first create a character array.
char[] arr = { 'p', 'q', 'r', 's' };
The method valueOf() will convert the entire array into a string.
String str = String.valueOf(arr);
The following is an example.
Example
public class Demo { public static void main(String []args) { char[] arr = { 'p', 'q', 'r', 's' }; String str = String.valueOf(arr); System.out.println(str); } }
Output
Pqrs
Let us see another example that use copyValueOf() method that convert char array to string.
Example
public class Demo { public static void main(String []args) { char[] arr = { 'p', 'q', 'r', 's' }; String str = String.copyValueOf(arr, 1, 2); System.out.println(str); } }
Output
Qr
Advertisements