EEsparaquia
EEsparaquia

Reputation: 200

How to exchange the space for backslash and a double quote using replace in Java

I'm coding a Java app to store paths in a particular format, so I need to escape some characters in order to put the paths in a database, but I cannot do it properly: The original string looks like this:

ML Database Prototype\\NAS-500\\

and I need it in this particular format:

"\"ML\ Database\ Prototype\\NAS-500\""

So far I'm trying to do it using

String str = "ML Database Prototype\\NAS-500\\";

newStr = ( "\"\""+str+"\"" ).replace(" ","\" ");

System.out.println(newStr);

""WT" Database" Prototype\\DR0151-populated"

Upvotes: 0

Views: 105

Answers (1)

EEstereo
EEstereo

Reputation: 81

You can use as follows and will work:

newStr = ( "\"\\\""+str+"\\\"\"" ).replace(" ","\\ ");

The output for this is:

"\"ML\ Database\ Prototype\\NAS-500\""

Upvotes: 1

Related Questions