Replace backslash character to slash in a String with JAVA
by admin on Aug.20, 2009, under General
Today I had a small problem with java, and I leave here my solution to help others who are in the same situation.
Here I give you a small trick to replace backslash special caracter to another caracter in Java. If you are doing some project, and you need to replace a backslash “\” with the forward slash “/”, you will notice you can´t do this in the normal way.
Let´s see “the normal way”:
address = address.replace("\" , "/"):
you will have a error.
try this….
String backslash= System.getProperty("file.separator") ;
address= address.replace(backslash,"/");
System.out.println(address);
OUTPUT:
C:/Program Files/Java/jre6
October 14th, 2010 on 13:34
I tried in many website. But yours very nice it’s working.
May 19th, 2011 on 07:54
It is possible if address is obtained as an environment variable in your code ie
String address = System.getenv(“ART_HOME”);
String backslash= System.getProperty(“file.separator”) ;
address= address.replace(backslash,”/”);
System.out.println(address);
ART_HOME=D:\TEST
Output: D:/TEST
December 10th, 2011 on 17:40
Many Many thanks………