Split a String by Comma and Space in Java
The following example code shows how to split a String by comma and space in Java:
String stringExample =
"[email protected],[email protected], [email protected], [email protected]";
String[] splitedArray = stringExample.split("(\\s+,\\s+)|(\\s+,)|(,\\s+)|,|(\\s+)");
System.out.println("Splited String to Array : " + Arrays.toString(splitedArray));
System.out.println("Total number of elements : " + splitedArray.length);
The above code will give the following output:
Splited String to Array : [[email protected], [email protected], [email protected], [email protected]]
Total number of elements : 4
Total number of elements : 4