We can use the String.valueOf() to convert a char array to string, but some times we need to specify the part of array to be copied, in this case, we can use String.copyValueOf() to do it.
First,let’s show you the example that convert a char array to string by String.valueOf() ;
1 2 3 4 5 |
public void test() { char[] arr = { 'a', 's', 'e', 'n','v','i','e','w' }; String str = String.valueOf(arr); System.out.println(str); } |
The output is
1 |
asenview |
Let us see another example that use copyValueOf() method that convert char array to string.
1 2 3 4 5 |
public void test() { char[] arr = { 'a', 's', 'e', 'n','v','i','e','w' }; String str = String.copyValueOf(arr,4,4); System.out.println(str); } |
The output is
1 |
view |