Skip to content

Split and join

Using split

In Java, the String class has a split() method that breaks a string into an array (actually a String[] in Java) based on a specified delimiter.

java
String genresString = "Fantasy,Sci-Fi,Mystery,Romance";
String[] genresArray = genresString.split(",");
System.out.println(Arrays.toString(genresArray));
console
[Fantasy, Sci-Fi, Mystery, Romance]

Individual letters

If we pass an empty string as our split argument, we get an array with all the letters of the original string.

java
String bookTitle = "Persepolis";
String[] titleLetters = bookTitle.split("");
System.out.println(Arrays.toString(titleLetters));
console
[P, e, r, s, e, p, o, l, i, s]

Join

The opposite of splitting is joining. We specify a string to sandwich between the items in an array, and the array is joined into a single string.

java
String[] bookTitles = {"The Great Gatsby", "To Kill a Mockingbird", "1984"};
String titlesString = String.join("; ", bookTitles);
System.out.println(titlesString);
console
The Great Gatsby; To Kill a Mockingbird; 1984