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 =
                "test1@example.com,test2@example.com, test3@example.com, test4@example.com";

        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 : [test1@example.com, test2@example.com, test3@example.com, test4@example.com]
Total number of elements : 4