Split the string on forward slash

java, split, string

Solution

There is no need to escape forward slashes. Your code works fine if you just do:

String[] paths = path.split("/");

Problem

I have a code which I wanted to split based on the forward slash "/". Whenever I have a regex split based on "////" it never splits and gives me the whole string back. I tried replacing with file separator which gives "\" and then splitting with "\\" works but not the below code. Below is the code tested ``` package org.saurav.simpletests.string; import java.io.File; public class StringManipulator { public static void main(String a[]){ String testString ="/UserId/XCode/deep"; //testString = testString.replace("/", File.separator); //testString = testString.replace("/", "_"); testSplitStrings(testString); } /** * Test the split string * @param path */ public static void testSplitStrings(String path){ System.out.println("splitting of sprint starts \n"); String[] paths = path.split("////"); for (int i = 0; i < paths.length; i++) { System.out.println("paths::"+i+" "+paths[i]+"\n"); } System.out.println("splitting of sprint ends"); } } ``` cheers, Saurav

Original source