Bookmark Topic Watch Topic
  • New Topic

Issue with String replacement

 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator



Where as the output i was expecting was:


What am i doing wrong here??

-Adi

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Can you provide an SSCCE please?
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 Linux VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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 Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Isn't this the same problem as http://www.coderanch.com/t/568814/java/java/Find-replace-pattern-String? You were already told to not use replaceAll but replace instead.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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.
 
    Bookmark Topic Watch Topic
  • New Topic