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

Issue with String replacement

 
Ranch Hand
Posts: 93
Eclipse IDE VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator



Where as the output i was expecting was:


What am i doing wrong here??

-Adi

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Can you provide an SSCCE please?
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Jeff Verdegan: does that make it better?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Aditya Sirohi wrote:Jeff Verdegan: does that make it better?



Did you click the link and read the page? I'm looking for something I can copy, paste, compile, and run, and get the same results as you.
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Aditya Sirohi wrote:




Without actually testing it, my first instinct says that the issue is at line 10 -- specifically the replacement string that was created at line 8. The second parameter to the replaceAll() method is not a regular java string -- it is a regex replacement string. With a regex replacement string, the backslash has special meaning, so if you want to replace with a literal backslash you need to escape the backslash.

Henry
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks, aah my eyes.. This is what happens when you work 2nights straight back on back

This change worked:- String replacedVal = remoteDirMSI.replaceAll("/", "\\\\\\\\");

Regards
Adi
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Aditya Sirohi wrote:Thanks, aah my eyes.. This is what happens when you work 2nights straight back on back

This change worked:- String replacedVal = remoteDirMSI.replaceAll("/", "\\\\\\\\");




Of course, the next question is... do you know why it is eight backslashes???

Anyway, it is probably easier if you use the built-in method to disable the regex replacement string features. To do this, change this...



to this...



Henry
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Henry Wong wrote:do you know why it is eight backslashes???


I didn't get this. Can you provide a short explanation (or links to it)?
Thanks.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Isn't this the same problem as https://coderanch.com/t/568814/java/java/Find-replace-pattern-String? You were already told to not use replaceAll but replace instead.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Rob Spoor wrote:Isn't this the same problem as . . .

Thank you for noticing, Rob. If this is a duplicate thread, then I shall close it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic