How to split a String in Java with delimiter?

To split (or parse) a String in Java, do this: string.split(",").

Here's how you do it:

// Example 1: Split by comma

String string = "One,Two,Three";
String[] pieces = string.split(",");

// [One, Two, Three]
System.out.println(java.util.Arrays.toString(pieces));
// Example 2: Split by dash surrounded by spaces

String string = "One - Two - Three";
String[] pieces = string.split(" - ");

// [One, Two, Three]
System.out.println(java.util.Arrays.toString(pieces));