Search code examples
javaregexreplacereplaceall

How to replace forward slash with triple forward slash in Java?


I am trying replace forward slash with a triple forward slash in my string

String path = “Resources/Menu/Data/Entities“

I want the output to look like this Resources///Menu///Date///Entities

I tried the below approach but none of them is working path = path.replaceAll(“/”,”///\”)

path = path.replaceAll(“/”, “\/\/\/”)

I did my research online but couldn’t find the solution. I know this looks like a really simple problem, but I can’t figure it out. Any help is appreciated.


Solution

  • Just use path.replaceAll("/", "///") without any backslashes. Forward slashes don't need escaping.