How to Split String on Forward Slash in Java

Published Jun 12, 2022

How can we split a string on the forward slash in Java?

Suppose we’re working with this string:

String str = "a/b/c";

We need an array with the elements: ["a", "b", "c"].

To do this, we can simply use split() with the forward slash as the delimiter. No need to escape the forward slash.

String[] arr = str.split("/");

Check out additional ways to split strings over some delimiter in Java.