• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting problem in converting the backward slash to forward slash

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic