Rose
Rose

Reputation: 1498

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.

Upvotes: 0

Views: 2386

Answers (2)

Mihir Patel
Mihir Patel

Reputation: 412

Use below code to get exact output that you want

String path = "Resources/Menu/Data/Entities";
String newPath = path.replaceAll("/", "///");
Log.d(TAG, "path :: " + path);
Log.d(TAG, "newPath :: " + newPath);

Output:

path :: Resources/Menu/Data/Entities

newPath :: Resources///Menu///Data///Entities

Upvotes: 0

shree.pat18
shree.pat18

Reputation: 21757

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

Upvotes: 5

Related Questions