Converting array to string
toString
he simplest way to convert an Array to String is by using the java.util.Arrays class. It has already a built in method toString().
1 2 3 4 5 6 7 8 9 10 |
import java.util.*; public class Main { public static void main(String[] args) { String[] str={"welcome","to","webencyclop"}; String arr=Arrays.toString(str); System.out.println(arr); } } |
Output:
1 |
[welcome, to, webencyclop] |
Converting String into array
split()
split method takes string as input and return string array.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.*; public class Main { public static void main(String[] args) { String str="welcome to webencyclop"; String[] arr=str.split(" "); for(int i=0;i<arr.length;i++) { System.out.println(arr[i]); } } } |
Output:
1 2 3 |
welcome to webencyclop |