• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting problem in converting the backward slash to forward slash  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
respected,
i have declared a string as
String str=c:\documents\myfile\xyz
all i want to convert the backward slash to forward slash.
but there is error in converting the backward slash to forward slash.
if any body can help me in ths regard.


biswajit
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

For us to explain the error, we'd have to see your code; I can show you the right way to do it, though:

String s = "C:\\windows\\system32\\drivers\\etc\\hosts";
s = s.replaceAll("\\\\", "/");

There are four backslashes there, because the backslash is a special character to both the Java compiler and the regular expression compiler. You have to double the backslash to tell the Java compiler to treat it as a plain character, and double it again to tell the regular expression compiler the same thing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!